PHP array intersection array_intersect (), array_intersect_assoc (), array_inte_PHP tutorial

Source: Internet
Author: User
The intersection of the PHP array array_intersect (), array_intersect_assoc (), and array_inte. 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 () the function returns an array (only an array worth comparing) with a common element of an intersection. the array_intersect_assoc () function binds the key value and value to compare the intersection part and the array_intersect_key () 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:

The code is as follows:


$ 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"
";
$ Num = array_intersect_assoc ($ array, $ array1 );
Print_r ($ num );
Echo"
";
$ Num = array_intersect_key ($ array, $ array1 );
Print_r ($ num );
?>


Running result:

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

Struct (only array worthy of comparison), the array_intersect_assoc () function binds the key value and value, compares the intersection part together, and the array_intersect_key () function is...

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.