tags:

views:

80

answers:

3

Here's the situations: I have 2 arrays, eg:

$a=array('a','b','c','d');
$b=array('1','b','c','e');

I want to produce 2 arrays with result:

$c=array('a','d');//only element appeared on $a
$d=array('1','e');//only element appeared on $b

Do you have a clever solution?

+2  A: 
$c = array_diff($a, $b);
$d = array_diff($b, $a);
deceze
A: 

Sorry, my bad. It turn out a was giving the wrong array in my test. simple array_diff solved the problem: $c = array_diff($a, $b); $d = array_diff($b, $a);

silent
A: 

Try using the array_diff() function:

array_diff(array1,array2,array3...)

eg:

<?php
$a1=array(0=>"Cat",1=>"Dog",2=>"Horse");
$a2=array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_diff($a1,$a2));
?>

Output:

Array ( [0] => Cat )

Source: http://www.w3schools.com/PHP/func_array_diff.asp

iano