PHP array intersection array_intersect (), Array_inter_key () functions such as small problems _php tutorial

Source: Internet
Author: User
The intersection of two arrays can be implemented using Array_intersect (), Array_inersect_assoc,array_intersect_key, where the Array_intersect () function is the intersection of two numbers, Returns an array of common elements for the intersection (only the array is worth comparing), the ARRAY_INTERSECT_ASSOC () function is binding the key and value, and the Array_intersect_key () function compares the key values of two arrays. Returns an array of key-value intersections.

But the actual application also encountered some small problems, is as follows:

Instance:


"Red", "green" and "red4", "Red15" and "Red",7=> "level", "Width" = "Red", "azzzz1" and "Art", "Peak" =>158); $array 1 = Array ("Red" = "Red2", "Greena" and "Red", "Red15" and "Red",7=> "level", "Width" = "Red", "azzzz" = > "Art", "Peak" =>158); $num = Array_intersect ($array, $array 1); Print_r ($num); echo "
"; $num = Array_intersect_assoc ($array, $array 1); Print_r ($num); echo "
"; $num = Array_intersect_key ($array, $array 1); Print_r ($num);?>
Operation Result:


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 comparison of the 1.array_intersect () function only compares the values of the array, and there is a "red" when compared with "red" and "Red2", whereas the "Red2" is not returned;

The 2.ARRAY_INTERSECT_ASSOC () function compares the value of an array with a key value, and there is no array_intersect, which is appropriate for a more rigorous comparison;

The 3.array_intersect_key () function is useful for comparing the intersection of two array key values, returning not only the key value, but the key value and the corresponding array value.

http://www.bkjia.com/PHPjc/478803.html www.bkjia.com true http://www.bkjia.com/PHPjc/478803.html techarticle the intersection of two arrays can be implemented using Array_intersect (), Array_inersect_assoc,array_intersect_key, where the Array_intersect () function is the intersection of two numbers, Return a cross ...

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