Small issues with the intersection of PHP array array_intersect (), array_intersect_assoc (), and array_inter_key () Functions

Source: Internet
Author: User

Returns an array of all elements in the intersection (only the array is worth comparing). The array_intersect_assoc () function binds the key value and value to compare the intersection part and the array_intersect_key () function () A function is an array that compares the key values of two arrays and returns the intersection of key values.
However, some minor problems have also been encountered in practical applications, as shown below:
Instance: Copy codeThe Code is as follows: <? PHP
$ Array = array ("red" => "Red", "green" => "red4", "Red15" => "Red", 7 => "Level ", "Width" => "Red", "azzzz1" => "art", "peak" => 158 );
$ Array1 = array ("red" => "Red2", "greena" => "red", "Red15" => "Red", 7 => "Level ", "Width" => "Red", "azzzz" => "art", "peak" => 158 );
$ Num = array_intersect ($ array, $ array1 );
Print_r ($ num );
Echo "<br/> ";
$ Num = array_intersect_assoc ($ array, $ array1 );
Print_r ($ num );
Echo "<br/> ";
$ Num = array_intersect_key ($ array, $ array1 );
Print_r ($ num );
?>

Running result:Copy codeThe Code is as follows: array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [azzzz1] => art [peak] => 158)
Array ([Red15] => Red [7] => Level [Width] => Red [peak] => 158)
Array ([red] => Red [Red15] => Red [7] => Level [Width] => Red [peak] => 158)

Summary:
1. the array_intersect () function only compares the array values. If "Red" and "Red2" are compared, "Red" is returned. Otherwise, "Red2" is not returned ";
2. The array_intersect_assoc () function compares the array value with the key value without the case of array_intersect. This function is suitable for strict comparison;
3. The array_intersect_key () function is applicable to comparing the intersection of two array key values. instead of only the key value, it returns the key value and the corresponding array value.

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.