In PHP, the array function is really very much, the actual work to learn more about some of PHP's own existing functions, will greatly improve the speed of work. This article shares the function of how to handle the set, intersection, and difference of two or more arrays in PHP.
(1) PHP calculates the set of two or more arrays
The combined set of two or more numbers is the result set of an array. Merging arrays in PHP generally uses both the Array_merge and the + operations. As for the difference between the two, you can refer to this site article:
PHP Merge array + vs. Array_merge
(1) PHP calculates the intersection of two or more arrays
An intersection is a collection of data that is part of two or more arrays. The intersection of the computed array mainly uses the function of the Array_intersect system, which is listed as follows:
Array_intersect ($arr, $arr 2[...]) returns an array $arr the intersection of the other arrays, with the key name unchanged.
Array_intersect_assoc ($arr, $arr 2[...]) returns an array $arr the intersection of the other arrays, while comparing the key names, the index is unchanged.
Array_intersect_uassoc ($arr, $arr 2 [...], ' cmp_function ') with an index check the intersection of an array, using a callback function, to compare the index.
Array_intersect_key ($arr, $arr 2 [...]) compares the intersection of an array with a key name.
Array_intersect_ukey ($arr, $arr 2 [...], ' cmp_function '); Use the callback function to compare the key names to calculate the intersection of the arrays.
Array_uintersect ($arr, $arr 2 [...], ' cmp_function ') compare the intersection of the array with the callback function to compare the data.
Array_uintersect_assoc ($arr, $arr 2[...], ' cmp_function ') compare the data with the callback function with the intersection of the Index check array.
Array_uintersect_uassoc ($arr, $arr 2 [...], ' cmp_function ') with an index to check the intersection of the array, use the callback function to compare the data and the index.
The Array_intersect example is as follows:
$array 1=array (' a ' = ' green ', ' red ', ' Blue '); $array 2=array (' b ' = ' green ', ' yellow ', ' red '); $result =array_intersect ($array 1, $array 2);
The results of the $result will be:
Array ( [A]=>green [0]=>red]
(1) PHP calculates the difference between two or more arrays
The difference set is the collection of data that is removed from that part of the intersection. The difference set of the computed array is mainly used in the functions of the Array_diff system, listed as follows:
Array_diff ($arr, $arr 2[...]) returns an array that contains all the values in $ARR1 but not in any other parameter array, with the key name unchanged.
Array_diff_uassoc ($arr, $arr 2, [...], ' cmp_function ') uses a callback function to index the difference set in the array.
Array_diff_assoc ($arr, $arr 2[...]) returns an array that $arr the difference from other arrays, while comparing the key names, the index is unchanged.
Array_udiff ($arr, $arr 2 [...], ' cmp_function ') compares the data with the callback function to calculate the difference set of the array.
Array_udiff_uassoc ($arr, $arr 2, [...], ' cmp_function ') the index checks the difference set of the array, and uses the callback function to compare the data and the index.
Array_udiff_assoc ($arr, $arr 2, [...], ' cmp_function ') with the index check the difference set of the array, using the callback function to compare the data, the key name is also compared.
The Array_diff example is as follows:
$array 1=array (' a ' = ' green ', ' red ', ' blue ', ' red '); $array 2=array (' b ' = ' green ', ' yellow ', ' red '); $result =array_diff ($array 1, $array 2);
The results of the $result will be:
Array (1=> ' blue ')
The above is the PHP array of the set, intersection and a simple introduction to the difference set function, the specific use you can refer to the PHP manual.