Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove common items from multiple arrays

Tags:

arrays

php

$array1 = array("a","b");

$array2 = array("b","c");

I want the following array from above arrays containing items which are not common in both arrays.

$output = array("a","c");

I have tried folloing

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

How can do it. Thanks.


1 Answers

Just make subtraction of union and intersection:

enter image description here

$array1 = array("a","b");    
$array2 = array("b","c");

$output = array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
like image 132
Alma Do Avatar answered Mar 23 '26 23:03

Alma Do



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!