The intersection of the PHP array Array_intersect (), Array_intersect_assoc (), Array_inter_key () function of the small problem _php tips

Source: Internet
Author: User
Returns an array of common elements of the intersection (just the array is worth comparing), the ARRAY_INTERSECT_ASSOC () function is to bind the key value and value, compare the intersection part, the Array_intersect_key () function is to compare the key values of the two arrays. Returns an array of key value intersections.
But the actual application also encountered some minor problems, is as follows:
Instance:
Copy Code code as follows:

? Php
$array = Array ("Red" => "red", "green" => "Red4", "Red15" => "Red",7=> "level", "Width" => "Red", "azzzz1" = > "Art", "Peak" =>158);
$array 1 = Array ("Red" => "Red2", "Greena" => "Red", "Red15" => "Red",7=> "level", "Width" => "Red", "azzzz" = > "Art", "Peak" =>158);
$num = Array_intersect ($array, $array 1);
Print_r ($num);
echo "<br/>";
$num = Array_intersect_assoc ($array, $array 1);
Print_r ($num);
echo "<br/>";
$num = Array_intersect_key ($array, $array 1);
Print_r ($num);
?>

Run Result:
Copy Code code 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)

Summarize:
The 1.array_intersect () function compares only the array values, but also returns "red" when compared to "red" and "Red2", whereas it does not return "RED2";
The 2.ARRAY_INTERSECT_ASSOC () function compares the value of an array with a key value and does not exist in a array_intersect case, and is suitable for a more rigorous comparison;
The 3.array_intersect_key () function is used to compare the intersection of two array key values, not just the key values, but the key values and the corresponding array values.

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.