PHP two array cross-difference set

Source: Internet
Author: User
PHP two array intersection difference set
Array_intersect--Explanation of the intersection of computed arrays
Array Array_intersect (array array1, array array2 [, array ...])


Array_intersect () returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note The key name remains unchanged.

Example 1. Array_intersect () example

$array 1 = Array ("A" = "green", "Red", "blue");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
?>

This makes $result become:

Array
(
[A] = Green
[0] = Red
)

Array_diff--A description of the difference set of the computed array
Array Array_diff (array array1, array array2 [, array ...])


Array_diff () returns an array that contains all the values in array1 but not in any other parameter array. Note The key name remains unchanged.

Example 1. Array_diff () example

$array 1 = Array ("A" = "green", "Red", "Blue", "Red");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);
?>


This makes the $result an array (1 = "blue");. Values that occur more than once in $array 1 are treated the same.

Note: Two units are considered identical only if (string) $elem 1 = = = (string) $elem 2 o'clock. That is, when the expression of a string is the same.
Note: Notice that this function checks only one dimension in a multidimensional array. Of course you can use Array_diff ($array 1[0], $array 2[0]); Check for deeper dimensions.

ARRAY_DIFF_ASSOC--the difference set of the computed array with index check

Description
Array Array_diff_assoc (array array1, array array2 [, array ...])


ARRAY_DIFF_ASSOC () returns an array that contains all the values in array1 but not in any other parameter array. Note that unlike Array_diff (), the key name is also used for comparison.

Example 1. ARRAY_DIFF_ASSOC () example

$array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
$array 2 = Array ("A" = "green", "yellow", "red");
$result = Array_diff_assoc ($array 1, $array 2);

/* The result is:
Array
(
[B] = Brown
[C] = Blue
[0] = Red
)
*/
?>


In the above example, you can see that the key value pair "a" = "green" is in two arrays, so it is not in the output of this function. In contrast, the key-value pair 0 = "Red" appears in the output because the key name of "Red" in the second parameter is 1.

The key value pair key = Two values in value are only in (string) $elem 1 = = = (string) $elem 2 o'clock are considered equal. This means that strict checks are used, and the expression of the strings must be the same.
  • 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.