1

Say I have two arrays:

     $a = a,b,c;
     $b = a,b;

When I compare this array output should be c.

Omit the common values in both array.

matino
  • 17,199
  • 8
  • 49
  • 58
  • 7
    [`array_diff`](http://php.net/manual/en/function.array-diff.php) – Jon Nov 29 '11 at 11:21
  • possible duplicate of [Compare to values of two arrays in PHP](http://stackoverflow.com/questions/2631520/compare-to-values-of-two-arrays-in-php) – Ash Burlaczenko Nov 29 '11 at 11:24
  • possible duplicate of [How can I compare two arrays and list differences in PHP?](http://stackoverflow.com/questions/504616/how-can-i-compare-two-arrays-and-list-differences-in-php) – Gumbo Nov 29 '11 at 11:27

4 Answers4

3

The quick answer:

array_merge(array_diff($a, $b), array_diff($b, $a));

array-diff($a, $b) will only extract values from $a which are not in $b.

The idea is to merge the differences.

And another way to achieve your goal might be:

function array_unique_merge() {
        return array_unique(call_user_func_array('array_merge', func_get_args()));
    }
Narcis Radu
  • 2,519
  • 22
  • 33
1

Have a look at the PHP array_diff function.

$a = a,b,c;
$b = a,b;
$c = array_diff($a,$b);
Nick
  • 6,316
  • 2
  • 29
  • 47
0

Firstly, that's not valid PHP - but...

$a = array("a","b","c");
$b = array("a","b");
print_r(array_diff($a,$b)); // Array ( [2] => c )
Prisoner
  • 27,391
  • 11
  • 73
  • 102
  • :I did it in the same way but after this i passed to implode,o/p of implode s array.How come? –  Nov 29 '11 at 11:35
0

Just to make things more straighforward

$a = array("a","b","c");
$b = array("a","b");
$new_array = array_merge(array_diff($a, $b), array_diff($b, $a));
while (list ($key, $val) = each ($new_array)) {
echo $val;
} 
Yanki Twizzy
  • 7,771
  • 8
  • 41
  • 68