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

Source: Internet
Author: User
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 (), and array_dif.

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 ().

Differences 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 is repeated, the key value of the key is the value corresponding to the last key name (the latter overwrites the previous one). If the array is digitally indexed, the key name will be re-indexed in a continuous manner.

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. the code is as follows:

  1. $ A = array (1 => 'A', 'B', 'C ');
  2. $ B = array (1 => 'A', 2, 'C ');
  3. $ Union = array_merge ($ a, $ B );
  4. $ Plus = $ a + $ B;
  5. Print_r ($ union );
  6. Print_r ($ plus );
  7. // The result is as follows:
  8. Array
  9. {
  10. [0] =>
  11. [1] => B
  12. [2] => c
  13. [3] => aa
  14. [4] => 2
  15. [5] => c
  16. }
  17. Array
  18. (
  19. [1] =>
  20. [2] => B
  21. [3] => c
  22. )

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:

  1. $ A2 = array ('str' => 'A', 'B', 'C ');
  2. $ B2 = array ('str' => 'A', 2, 'C ');
  3. $ Union2 = array_merge ($ a2, $ b2 );
  4. $ Plus2 = $ a2 + $ b2;
  5. Print_r ($ union2 );
  6. Print_r ($ plus2 );
  7. // The result is as follows:
  8. Array
  9. (
  10. [Str] => aa
  11. [0] => B
  12. [1] => c
  13. [2] => 2
  14. [3] => c
  15. )
  16. Array
  17. (
  18. [Str] =>
  19. [0] => B
  20. [1] => c
  21. )

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 an 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:

  1. $ A = array ('jpg ', 'PNG', 'GIF', 'bmp ');
  2. $ B = array ('jpg ', 'txt', 'docx', 'bmp ');
  3. $ 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:

  1. $ Intersection2 = array_intersect (array_map ('strlower ', $ a), array_map ('strlower', $ B ));
  2. Print_r ($ intersection );
  3. Print_r ($ intersection2 );
  4. // The result is as follows:
  5. Array
  6. (
  7. [3] => bmp
  8. )
  9. Array (
  10. [0] => jpg
  11. [3] => bmp
  12. )

Calculate the difference set of the array,The code is as follows:

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

Note: The Returned result element contains $ old, excluding $ new.

Print_r ($ difference );

Result:

  1. Array
  2. (
  3. [0] => jpg
  4. [1] => png
  5. [2] => gif
  6. )

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 names remain unchanged.

Syntax: array_diff (array1, array2, array3 ...)

The code is as follows:

  1. $ Difference = array_diff (array_map ('strtolower', $ old ),
  2. Array_map ('strlower ', $ new ));

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.