Home >Backend Development >PHP Tutorial >Regarding the problem of poor collection communication between array_diff and array_intersect in PHP
Excuse me when defining the following content
<code>$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result);</code>
The returned difference set is Array ([d] => yellow)
But if they are reversed and $a2 is placed in front, why can’t the difference set be found
<code>$result=array_diff($a2,$a1);</code>
Excuse me when defining the following content
<code>$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result);</code>
The returned difference set is Array ([d] => yellow)
But if they are reversed and $a2 is placed in front, why can’t the difference set be found
<code>$result=array_diff($a2,$a1);</code>
difference sets are in order, just like 4 - 3
is not equal to 3 - 4
. This example is not very reasonable, so you will think that you can't find the difference set. In fact, the difference set between $a2
and $a1
is empty, so it looks like there is no difference set. You can use $b = array("e"=>"red","f"=>"green","g"=>"blue","h"=>"gray");
so you can immediately understand what the difference set is.
array array_diff ( array $array1 , array $array2 )
Difference returns values that are in array1 but not in array2 and any other parameter arrays.
After the inversion, of course there is no more
Read the manual carefully
http://www.w3school.com.cn/php/func_array_diff.asp
Thanks for the invitation. The references from the people above are enough for you. array_diff