Home  >  Article  >  Backend Development  >  What is the method to obtain the intersection and difference of arrays in php

What is the method to obtain the intersection and difference of arrays in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-28 16:09:422276browse

What is the method to obtain the intersection and difference of arrays in php

1. Intersection of arrays array_intersect()

array_intersect() function returns an array with retained keys, This array consists 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 the 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 )
?>

Only in the two elements Only when they are equal and have the same data type, the array_intersect() function will consider them to be the same.

Related recommendations: "PHP Tutorial"

Intersection of associative arrays array_intersect_assoc()

Function array_intersect_assoc() and array_intersect () is basically the same, 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 form 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. Array difference array_diff()

Function array_diff() returns values ​​that appear in the first array but are not in other input arrays. This function is the opposite of array_intersect().

array array_diff(array array1,array array2[,arrayN…])

The 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 )
?>

The difference set of associative array array_diff_assoc()

The function array_diff_assoc() is basically the same as array_diff(), except that it The keys of the array are also taken into account 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 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_diff_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [yellow] => Banana )
?>

The above is the detailed content of What is the method to obtain the intersection and difference of arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn