Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using array_diff(), how to get difference from array2 instead of array1?

I'm trying to use array_diff like so. Here are my two array outputs:

List 1 Output

Array ([0] => 0022806 ) 

List 2 Output

Array ([0] => 0022806 [1] => 0023199 ) 

PHP

$diff = array_diff($list_1, $list_2);

print "DIFF: " . count($diff) ."<br>";
print_r($diff);

The Output is:

DIFF: 0
Array ( )

Any idea what I'm doing wrong? Why is 0023199 not returned?

like image 900
user1216398 Avatar asked Oct 16 '12 18:10

user1216398


2 Answers

The order of arguments in array_diff() is important

Returns an array containing all the entries from array1 that are not present in any of the other arrays

like image 71
Mark Baker Avatar answered Nov 15 '22 17:11

Mark Baker


try;

$diff = array_merge(array_diff($list_1, $list_2), array_diff($list_2, $list_1));

print "DIFF: " . count($diff) ."<br>";
print_r($diff);
like image 44
Mete Yarıcı Avatar answered Nov 15 '22 17:11

Mete Yarıcı