tags:

views:

1655

answers:

3

How else might you compare two arrays ($A and $B )and reduce matching elements out of the first to prep for the next loop over the array $A?

$A = array(1,2,3,4,5,6,7,8);
$B = array(1,2,3,4);

$C = array_intersect($A,$B);  //equals (1,2,3,4)
$A = array_diff($A,$B);       //equals (5,6,7,8)

Is this the simplest way or is there a way to use another function that I haven't thought of? My goal is to have an array that I can loop over, pulling out groups of related content (I have defined those relationships elsewhere) until the array returns false.

A: 

You've got it. Just use array_diff or array_intersect. Doesn't get much easier than that.

gaoshan88
+1  A: 

See also array_unique. If you concatenate the two arrays, it will then yank all duplicates.

warren
A: 

Hey, even better solution: array _ uintersect. This let's you compare the arrays as per array_intersect but then it lets you compare the data with a callback function.

gaoshan88