One day learning a new PHP function (2) Array_diff ()/array_diff_key ()/array_diff_assoc () _php Tutorial

Source: Internet
Author: User

One day learning a new PHP function (2) Array_diff ()/array_diff_key ()/array_diff_assoc ()


Array_diff (array array1, array array2 [, array ...])

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

Liezi


  
    "green", "red", "blue", "red");    $array2 = array("b" => "green", "yellow", "red");    $result = array_diff($array1, $array2);    print_r($result);?>

The output result is

Array([1] => blue)

Attention

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 that this function examines 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 Array_diff_assoc (array array1, array array2 [, array ...])

Description
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.
Liezi


  
    "green", "b" => "brown", "c" => "blue", "red");    $array2 = array("a" => "green", "yellow", "red");    $result = array_diff_assoc($array1, $array2);    

Output

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.

2


  
   

Output

Array(    [0] => 0    [1] => 1)    

The key value pair key = Two values in value are considered equal only when (string) elem1=== (String) is elem2. This means that strict checks are used, and the expression of the strings must be the same.

Attention

Note: Notice that this function examines only one dimension in a multidimensional array. Of course you can use ARRAY_DIFF_ASSOC ($array 1[0], $array 2[0]); Check for deeper dimensions.

Array Array_diff_key (array $array 1, array $array 2 [, array $ ...])

Description
Returns an entry for a different key name, based on the key name and Array2 in Array1. This function is the same as Array_diff () except that the comparison is based on the key name and not the value.
Returns an array that contains all the values that appear in array1 but do not appear in any of the other parameter array's key names.

Liezi


  
    1, 'red'  => 2, 'green'  => 3, 'purple' => 4);    $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);    

Output

array(2) {  ["red"]=>  int(2)  ["purple"]=>  int(4)}

The two key names in key = value pair are only in (string) $key 1 = = = (string) $key 2 o'clock are considered equal. In other words, strict type checking is performed, so the expression of the string must be exactly the same.

Array Array_intersect (array $array 1, array $array 2 [, array $ ...])

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

http://www.bkjia.com/PHPjc/997429.html www.bkjia.com true http://www.bkjia.com/PHPjc/997429.html techarticle every day one learns a new PHP function (2) Array_diff ()/array_diff_key ()/array_diff_assoc () Array_diff (array array1, array array2 [, Array]) description Array_diff () returns an array ...

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