For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > June 2007 > filter on two array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author filter on two array
artev

2007-06-23, 10:01 pm

two array
A) 1, 2, 3, 4, 5, 6
B) 4, 6, 9

how can to have an array
C) 4, 6
all the B's values that have value in A

and
an array
D) 1, 2, 3, 4, 5, 6, 9
a simple union of A + B with no duplicates values

peter

2007-06-23, 10:01 pm

> two array
> A) 1, 2, 3, 4, 5, 6
> B) 4, 6, 9
>
> how can to have an array
> C) 4, 6
> all the B's values that have value in A
>
> and
> an array
> D) 1, 2, 3, 4, 5, 6, 9
> a simple union of A + B with no duplicates values


not necessarily the best solution but something like the following works:-

<?php

$a = array(1,2,3,4,5,6);
$b = array(4,6,9);
$c = array();
$d = $a;
foreach ($b as $value)
{
if (in_array($value, $a))
{
$c[] = $value;
}
else
{
$d[] = $value;
}
}
print_r($c);
print_r($d);
?>

this of course assumes that there will be no duplicate entries in $a already


Rik

2007-06-23, 10:01 pm

On Sat, 23 Jun 2007 12:16:11 +0200, artev <mailnotspammm@notspamm.nn>
wrote:

> two array
> A) 1, 2, 3, 4, 5, 6
> B) 4, 6, 9
>
> how can to have an array
> C) 4, 6
> all the B's values that have value in A
>
> and
> an array
> D) 1, 2, 3, 4, 5, 6, 9
> a simple union of A + B with no duplicates values


Please don't mulitpost, thank you.

Answered in alt.php

--
Rik Wasmus
Guy

2007-06-24, 7:58 am

artev a écrit :
> two array
> A) 1, 2, 3, 4, 5, 6
> B) 4, 6, 9
>
> how can to have an array
> C) 4, 6
> all the B's values that have value in A


$C=array_intersect($A,$B)

>
> and
> an array
> D) 1, 2, 3, 4, 5, 6, 9
> a simple union of A + B with no duplicates values


$D=array_unique(array_merge($A,$B))

GR
>

Ilona

2007-06-30, 2:55 am

Christina Applegate and Halle Berry licking each other all over by the truck!
Wildchild

2007-06-30, 11:47 pm

I have uploaded my private x x x videos ;)
Click here to watch
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com