Introduction to the parallel set, intersection and difference set functions of arrays in PHP

Source: Internet
Author: User
Tags array arrays functions key

The array function in PHP is really very much, the actual work to learn more about some of the functions of PHP itself, will greatly improve the speed of work. This article shares a function of how to handle two or more arrays of sets, intersections, and difference sets in PHP.

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

A set of two or more numbers is combined into a result set of an array. Merging arrays in PHP typically uses both Array_merge and + operations. As for the difference between the two, you can refer to this site article:

The difference between the PHP merge array + and the 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. Computing the intersection of arrays is mainly used in the Array_intersect system functions, listed as follows:

Array_intersect ($arr, $arr 2[...]) Returns the intersection of an array $arr with other arrays, and the key name does not change.
Array_intersect_assoc ($arr, $arr 2[...]) returns an array $arr the intersection of the other arrays, comparing the key names and the indexes unchanged.
Array_intersect_uassoc ($arr, $arr 2 [...], ' cmp_function ') check the intersection of the array with the index, and use the callback function to compare the index.
Array_intersect_key ($arr, $arr 2 [...]) uses the key name to compare the intersection of the computed array.
Array_intersect_ukey ($arr, $arr 2 [...], ' cmp_function '); Computes the intersection of the array using the callback function to compare the key name.

Array_uintersect ($arr, $arr 2 [...], ' cmp_function ') compares the intersection in the array with the callback function to compare the data.
Array_uintersect_assoc ($arr, $arr 2[...], ' cmp_function ') the intersection of an array with an indexed check compares data with a callback function.
Array_uintersect_uassoc ($arr, $arr 2 [...], ' cmp_function ') check the intersection of the array with the index, and compare the data and index with the callback function.

Array_intersect examples are 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 set of two or more arrays

The difference set is the collection of data that removes the part of the intersection. The difference set of the computed array is mainly used in the function of the Array_diff system, which is listed as follows:

Array_diff ($arr, $arr 2[...]) Returns an array that includes all values in $ARR1 but not in any other parameter array, and the key name does not change.
Array_diff_uassoc ($arr, $arr 2, [...], ' cmp_function ') uses the callback function to compare the difference set in the array.
Array_diff_assoc ($arr, $arr 2[...]) returns an array $arr the difference set with other arrays, comparing the key names and the index unchanged.

Array_udiff ($arr, $arr 2 [...], ' cmp_function ') compares the data with the callback function to compute the difference set of the array.
Array_udiff_uassoc ($arr, $arr 2, [...] , ' cmp_function ') with an index to check the difference set of the array, using the callback function to compare data and indexes.
Array_udiff_assoc ($arr, $arr 2, [...], ' cmp_function ') sets the difference set with an indexed check array, compares the data with the callback function, and compares the key names.

Array_diff examples are 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 function is just a rough introduction of the function name and role, as for the specific use of each function, please refer to the PHP manual.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.