The example in this article describes the method of obtaining the intersection and difference of arrays in PHP. Share it with everyone for your reference. The specific analysis is as follows:
1. Intersection of arrays array_intersect()
The array_intersect() function returns a key-preserved array consisting only of values that appear in the first array and appear in every other input array. Its form is as follows:
array array_intersect(array array1,array array2[,arrayN…])
The following example will return 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 will consider two elements to be the same only if they are equal and have the same data type.
Intersection of associative arrays array_intersect_assoc()
The function array_intersect_assoc() is basically the same as array_intersect(), except that it also considers the keys of the array in the comparison. Therefore, only key/value pairs that appear in the first array and also appear in all other input arrays are returned in 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 also appear in $fruit2 and $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_intersect_assoc($fruit1, $fruit2, $fruit3); print_r($intersection); // output // Array ( [red] => Apple ) ?>
2. Difference of arrays array_diff()
The function array_diff() returns values that appear in the first array but not in other input arrays. This function is the opposite of array_intersect().
array array_diff(array array1,array array2[,arrayN…])
Examples are as follows:
<?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 ) ?>
Difference of associative arrays array_diff_assoc()
The function array_diff_assoc() is basically the same as array_diff(), except that it also considers the keys of the array when comparing. Therefore, only key/value pairs that appear in the first array but not in the other input arrays are returned in the result array. Its form is as follows:
array array_diff_assoc(array array1,array array2[,arrayN…])
The following example only returns [yellow] => Banana, because this special key/value pair appears in $fruit1, but 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 be helpful to everyone’s PHP programming design.