Search code examples
phparrayscomparearray-difference

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?


Solution

  • 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