PHP array Difference value of four function learning

Source: Internet
Author: User
    1. $array 1 = Array ("A" = "green", "Red", "blue");
    2. $array 2 = Array ("b" = "green", "yellow", "red");
    3. $result = Array_intersect ($array 1, $array 2);
    4. ?>
Copy Code

The example above will output: Array ([a] = green[0] + red)

2.ARRAY_INTERSECT_ASSOC () returns a key-value pair with the same key and value in all arrays, based on the previous function.

Example:

    1. $array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
    2. $array 2 = Array ("A" = "green", "yellow", "red");
    3. $result _array = Array_intersect_assoc ($array 1, $array 2);
    4. ?>
Copy Code

The example above will output: Array ([a] = green)

3.array_diff () carries multiple arrays, returning a new array of all the values in the first array that are not in the following array, and the corresponding key is taken from the first array.

Example:

    1. $array 1 = Array ("A" = "green", "Red", "Blue", "Red");

    2. $array 2 = Array ("b" = "green", "yellow", "red");
    3. $result = Array_diff ($array 1, $array 2);

    4. Print_r ($result);

    5. ?>

Copy Code

The example above will output: Array ([1] = blue)

4.array_diff_assoc () on the basis of the previous function, it is necessary to match not only the value but also the key.

Example:

    1. $array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
    2. $array 2 = Array ("A" = "green", "yellow", "red");
    3. $result = Array_diff_assoc ($array 1, $array 2);
    4. ?>
Copy Code

The example above will output: Array ( = = Brown[c] = blue[0] = red)

  • 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.