Array differences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have the following scenario:

 

     $array1 = array("12", "34", "56", "78", "90");

     $array2 = array("12", "23", "56", "78", "89");

 

     $result = array_diff($array1, $array2);

 

     print_r($result);

 

 

This returns:

 

     Array

     (

         [1] => 34

         [4] => 90

     )

 

 

However what I really want is a two-way comparison.  I want elements that
don't exist in either to be returned:

 

34 and 90 because they don't exist in $array2, AND 23 and 89 because they
don't exist in $array1.  So, is that a two step process of first doing an
array_diff($array1, $array2) then reverse it by doing array_diff($array2,
$array1) and merge/unique the results?  Any caveats with that?

 

     $array1 = array("12", "34", "56", "78", "90");

     $array2 = array("12", "23", "56", "78", "89");

 

     $diff1 = array_diff($array1, $array2);

     $diff2 = array_diff($array2, $array1);

 

     $result = array_unique(array_merge($diff1, $diff2));

 

     print_r($result);

 

 

-- A


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux