Home >Backend Development >PHP Tutorial >Regarding the problem of poor collection communication between array_diff and array_intersect in PHP

Regarding the problem of poor collection communication between array_diff and array_intersect in PHP

WBOY
WBOYOriginal
2016-07-06 13:52:571098browse

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>

Reply content:

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How is urlencode() used?Next article:How is urlencode() used?