On the problem of comparison between Array_diff and Array_intersect in PHP in comparison with set communication

Source: Internet
Author: User
When defining the following, please

$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);

Returns the difference set to Array ([d] = = yellow)
But if we put them upside down, why can't we find the $a2 in front of them?

$result=array_diff($a2,$a1);

Reply content:

When defining the following, please

$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);

Returns the difference set to Array ([d] = = yellow)
But if we put them upside down, why can't we find the $a2 in front of them?

$result=array_diff($a2,$a1);

The difference sets are sequential, as if they were 4 - 3 not equal 3 - 4 . This example is not very reasonable, so you will feel that you can not find the difference set, in fact, $a2 and $a1 the difference is empty, so it looks like there is no difference set. You can make it $b = array("e"=>"red","f"=>"green","g"=>"blue","h"=>"gray"); so that you can immediately understand what the difference is.

Array Array_diff (array $array 1, array $array 2)
The comparison returns the values in Array1 but not in the array2 and any other parameter arrays.
After the reversal, of course there is no

Read the manual carefully

Http://www.w3school.com.cn/php/func_array_diff.asp

Thank you for inviting me. A few references upstairs are enough for you to use. Array_diff

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.