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)