Home > Backend Development > PHP Tutorial > How to get the intersection and difference of arrays in PHP, get the intersection of arrays in php_PHP tutorial

How to get the intersection and difference of arrays in PHP, get the intersection of arrays in php_PHP tutorial

WBOY
Release: 2016-07-13 09:51:19
Original
828 people have browsed it

How to obtain the intersection and difference of arrays in PHP, how to obtain the intersection and difference of arrays in PHP

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:

<&#63;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 )
&#63;>

Copy after login

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:

<&#63;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 )
&#63;>

Copy after login

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:

<&#63;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 )
&#63;>

Copy after login

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.

<&#63;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 )
&#63;>

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1014424.htmlTechArticleHow to get the intersection and difference of arrays in PHP, get the intersection of arrays in php. This article tells the example of getting the intersection and difference of arrays in PHP. set method. Share it with everyone for your reference. The specific analysis is as follows:...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template