Compare PHP to get two arrays of the same and different elements of a method

Source: Internet
Author: User
This article mainly to share with you to compare PHP to get two arrays of the same and different elements of the method, I hope to help everyone.

1. Get the same elements of an array

array_intersect () This function compares the key values of two (or more) arrays and returns an array of intersections that includes all the arrays in the comparison ( array1 ),

also in any other parameter array ( array2 or and so on) in the key value.

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("e" = "red", "f" = > "Green", "G" and "Blue"), $result =array_intersect ($a 1, $a 2);p Rint_r ($result),//array ([a] = red [b] = Green [c] = blue)

ARRAY_INTERSECT_ASSOC () function is used to compare the key names and key values of two (or more) arrays and return the intersection, unlike the Array_intersect () function, which, in addition to comparing key values,

also compares key names. The key name of the element in the returned array remains unchanged.

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("a" and "Red", "b" = > "Green", "c" = "blue"), $result =array_intersect_assoc ($a 1, $a 2);p Rint_r ($result);? >//array ([A] = red [b] = green [c] = blue)

2, getting different elements in an array

The Array_diff () function returns an array of difference sets for two arrays. The array includes all of the key values in the array being compared, but not in any other parameter array.

In the returned array, the key name remains unchanged.


<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("e" = "red", "f" = > "Green", "g" = "blue"), $result =array_diff ($a 1, $a 2);p Rint_r ($result);? >//array ([d] = yellow)

The ARRAY_DIFF_ASSOC () function is used to compare the key names and key values of two (or more) arrays and return a difference set.

<?php$a1=array ("A" = "red", "b" = "green", "c" = "Blue", "d" = "yellow"), $a 2=array ("a" and "Red", "b" = > "Green", "C" and "Blue"), $result =array_diff_assoc ($a 1, $a 2);p Rint_r ($result);//array ([d] = yellow)

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.