PHP obtains the array intersection and the difference set, and php obtains the array intersection.
This example describes how to obtain the intersection and difference set of arrays in PHP. Share it with you for your reference. The specific analysis is as follows:
I. array intersection array_intersect ()
The array_intersect () function returns an array with keys retained. This array is composed of only values that appear in the first array and appear in each other input array. The format is as follows:
Array array_intersect (array array1, array array2 [, arrayN…])
The following example returns all fruits that appear in the $ fruit1 array and also appear in $ fruit2 and $ fruit3:
<?php$fruit1 = array("Apple","Banana","Orange");$fruit2 = array("Pear","Apple","Grape");$fruit3 = array("Watermelon","Orange","Apple");$intersection = array_intersect($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [0] => Apple )?>
The array_intersect () function considers the two elements to be identical only when they have the same data type.
Intersection of correlated arrays array_intersect_assoc ()
The array_intersect_assoc () function is basically the same as the array_intersect () function, except that the array key is also considered during comparison. Therefore, only key/value pairs that appear in the first array and also appear in all other input arrays are returned to the result array.
The format is as follows:
Array array_intersect_assoc (array array1, array array2 [, arrayN…])
The following example returns all key/value pairs that appear in the $ fruit1 array and $ fruit2 and $ fruit3 at the same time:
<?php$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [red] => Apple )?>
2. array difference set array_diff ()
The array_diff () function returns a value that appears in the first array but does not exist in other input arrays. This function is opposite to array_intersect.
Array array_diff (array array1, array array2 [, arrayN…])
Example:
<?php$fruit1 = array("Apple","Banana","Orange");$fruit2 = array("Pear","Apple","Grape");$fruit3 = array("Watermelon","Orange","Apple");$intersection = array_diff($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [1] => Banana )?>
Array_diff_assoc ()
The array_diff_assoc () function is basically the same as the array_diff () function, except that the array key is also considered during comparison. Therefore, only key/value pairs that appear in the first array instead of other input arrays are returned to the result array. The format is as follows:
Array array_diff_assoc (array array1, array array2 [, arrayN…])
In the following example, only [yellow] => Banana is returned, because this special key/value pair exists in $ fruit1, and does not exist in $ fruit2 or $ fruit3.
<?php$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");$intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);print_r($intersection);// output// Array ( [yellow] => Banana )?>
I hope this article will help you with php programming.