Introduction to array union, intersection, and difference set functions in php _ PHP Tutorial

Source: Internet
Author: User
Php array union, intersection, and difference set functions. In php, if I want to perform operations on two arrays, such as union, intersection, and difference set, we can directly use the built-in functions of php to perform operations such as array_merge (), array_intersect (), array_diff (). in php, if I want to perform operations on two arrays, such as union, intersection, and difference set, we can directly use the built-in functions of php to perform operations such as array_merge (), array_intersect (), array_diff ().

// The difference between the combined array_merge and "+" of the calculated array
The array_merge () function combines two or more numbers into an array.

If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.

Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed. (See example 2)

The code is as follows:

$ A = array (1 => 'A', 'B', 'C ');
$ B = array (1 => 'A', 2, 'C ');
$ Union = array_merge ($ a, $ B );
$ Plus = $ a + $ B;
Print_r ($ union );
Print_r ($ plus );
The results are as follows:
Array
{
[0] =>
[1] => B
[2] => c
[3] => aa
[4] => 2
[5] => c
}
Array
(
[1] =>
[2] => B
[3] => c
)

When the two arrays to be merged have the same string key, using array_merge () will overwrite the original value,
When "+" is used to merge an array, the first value will be returned as the final result, just like the number key of the array merged with "+", as shown in the following example:

The code is as follows:

$ A2 = array ('str' => 'A', 'B', 'C ');
$ B2 = array ('str' => 'A', 2, 'C ');
$ Union2 = array_merge ($ a2, $ b2 );
$ Plus2 = $ a2 + $ b2;
Print_r ($ union2 );
Print_r ($ plus2 );

The results are as follows:

Array
(
[Str] => aa
[0] => B
[1] => c
[2] => 2
[3] => c
)
Array
(
[Str] =>
[0] => B
[1] => c
)

Note: If you want to use array_merge to merge two arrays, the returned results may have the same elements. in this case, you can use array_unique () to remove the same elements.

// Calculate the intersection of arrays

The array_intersect () function returns the intersection array of two or more arrays.

The result array contains all values in the compared array and all other parameter arrays. The key name remains unchanged.

Note: only values are used for comparison.

The code is as follows:
$ A = array ('jpg ', 'PNG', 'GIF', 'bmp ');
$ B = array ('jpg ', 'txt', 'docx', 'bmp ');
$ Intersection = array_intersect ($ a, $ B );


You can also use functions to obtain what you want (for example, the elements are case-insensitive)

The code is as follows:

$ Intersection2 = array_intersect (array_map ('strlower ', $ a), array_map ('strlower', $ B ));
Print_r ($ intersection );
Print_r ($ intersection2 );

The results are as follows:

The code is as follows:
Array
(
[3] => bmp
)
Array (
[0] => jpg
[3] => bmp
)


// Calculate the difference set of the array

The code is as follows:

$ Old = array ('jpg ', 'PNG', 'GIF', 'bmp ');
$ New = array ('jpg ', 'txt', 'docx', 'bmp ');
$ Difference = array_diff ($ old, $ new );

Note: The Returned result element contains $ old, excluding $ new.
Print_r ($ difference );
Result:

The code is as follows:
Array
(
[0] => jpg
[1] => png
[2] => gif
)

You can also use the function to perform processing first and then calculate the difference set.

The array_diff () function returns the number of difference sets of two arrays. This array includes all the key values in the compared array, but not in any other parameter array.

In the returned array, the key name remains unchanged.

Syntax
Array_diff (array1, array2, array3 ...)

The code is as follows:

$ Difference = array_diff (array_map ('strlower ', $ old), array_map ('strlower', $ new ));

Round (), array_intersect (), array_diff (). // count...

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.