Union, intersection and difference functions of arrays

小云云
Release: 2023-03-17 16:24:01
Original
3425 people have browsed it

There are really many array functions in php. In actual work, knowing more about some functions that exist in php itself will greatly improve the speed of work. This article shares the functions of how to handle the union, intersection and difference of two or more arrays in PHP.

(1) PHP calculates the union of two or more arrays

The union is the result set of combining two or more arrays into one array. In php, array_merge and + are generally used to merge arrays. As for the difference between the two, please refer to the article on this site:

The difference between PHP merging array + and array_merge

(1) PHP calculates the intersection of two or more arrays

Intersection is the set of data that exists in two or more arrays. Calculating the intersection of arrays mainly uses the functions of the array_intersect system, which are listed below:

array_intersect ( $arr , $arr2[……]) Returns the intersection of an array $arr and other arrays, and the key names remain unchanged.
array_intersect_assoc( $arr, $arr2[……]) Returns the intersection of an array $arr and other arrays, while comparing the key names and keeping the index unchanged.
array_intersect_uassoc( $arr , $arr2 [……] , 'cmp_function ') Checks the intersection of arrays with index, uses callback function, and compares the index.
array_intersect_key ( $arr ,$arr2 […] ) Calculates the intersection of arrays using key name comparison.
array_intersect_ukey( $arr , $arr2 […], 'cmp_function'); Use callback function to compare key names to calculate the intersection of arrays.

array_uintersect ( $arr , $arr2 [……] , 'cmp_function' ) Compare intersections in arrays and use callback functions to compare data.
array_uintersect_assoc( $arr, $arr2[……] , 'cmp_function') Check the intersection of arrays with index and compare data using callback function.
array_uintersect_uassoc($arr , $arr2 [……] , 'cmp_function' ) checks the intersection of arrays with indexes, and uses the callback function to compare data and indexes.

array_intersect example is as follows:

 $array1=array('a'=>'green','red','blue');    
$array2=array('b'=>'green','yellow','red');    
$result=array_intersect($array1,$array2);
Copy after login

The result of $result will be:

Array
(
   [a]=>green
   [0]=>red
)
Copy after login

(1) PHP calculates the difference set of two or more arrays

The difference set is the set of the part of the data excluding the intersection. Calculating the difference set of an array mainly uses the functions of the array_diff system, which are listed as follows:

array_diff( $arr , $arr2[...] ) Returns an array that includes everything in $arr1 but not in any other The values ​​in the parameter array, the key names remain unchanged.
array_diff_uassoc( $arr ,$arr2 ,[...] , 'cmp_function') uses the callback function as an index to compare the difference set in the array.
array_diff_assoc( $arr , $arr2[……] ) Returns an array, the difference between $arr and other arrays, and compares the key names at the same time, and the index remains unchanged.

array_udiff ( $arr , $arr2 […] , 'cmp_function') Use the callback function to compare data to calculate the difference of the array.
array_udiff_uassoc ( $arr ,$arr2 ,[...] , 'cmp_function') Check the difference set of the array with index, and use the callback function to compare the data and index.
array_udiff_assoc ( $arr ,$arr2 ,[……] , 'cmp_function') Check the difference set of the array with index, use the callback function to compare the data, and the key names are also compared.

array_diff example is as follows:

$array1=array('a'=>'green','red','blue','red');    
$array2=array('b'=>'green','yellow','red');    
$result=array_diff($array1,$array2);
Copy after login

The result of $result will be:

array(1=>'blue')

The above is the php array A brief introduction to the union, intersection and difference functions. For specific usage, you can refer to php manual.

Related recommendations:

PHP array simple intersection, difference and union function implementation examples

Summary about array union Notes

php Multiple array union, intersection and difference operation function summary

The above is the detailed content of Union, intersection and difference functions of arrays. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!