php - Get uncommon values from two or more arrays -


is there function in php give array of uncommon values 2 or more arrays?

for example:

$array1 = array( "green", "red", "blue"); $array2 = array( "green", "yellow", "red"); .... $result = function_needed($array1, $array2,...); print_r($result); 

should give output:

array("blue", "yellow", ...); 

use array_diff , array_merge:

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

here's demo.

for multiple arrays, combine callback , array_reduce:

function unique(&$a, $b) { return $a ? array_merge(array_diff($a, $b), array_diff($b, $a)) : $b; } $arrays = array( array('green', 'red', 'blue'), array('green', 'yellow', 'red') ); $result = array_reduce($arrays, 'unique'); 

and here's demo of that.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

Adding duplicate array rows in Php -