Php calculates the intersection of two arrays and two functions

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. Using array_intersect to calculate the intersection of two arrays is faster than using array_diff to obtain the union of the same two arrays. If the number of difference sets between array $ a and array $ B is required, use count ($)

Welcome to the Linux community forum and interact with 2 million technical staff> Use array_intersect to calculate the intersection of two arrays faster than use array_diff to obtain the union of the same two arrays. If the number of difference sets between array $ a and array $ B is required, use count ($ a)-count (array_intersect ($ a, $ B) instead

Welcome to the Linux community forum and interact with 2 million technicians>

Using array_intersect to calculate the intersection of two arrays is faster than using array_diff to obtain the union of the same two arrays.

If the number of difference sets between array $ a and array $ B is required, use count ($ a)-count (array_intersect ($ a, $ B )), instead of using count (array_diff ($ a, $ B ));

The front is faster than the latter, and more obvious in the large array.

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.

Syntax

Array_intersect (array1, array2, array3 ...) Parameter description

Array1 is required. The first array to be compared with other arrays.

Array2 is required. The array to be compared with the first array.

Array3 is optional. The array to be compared with the first array. There can be multiple.

Instance

  

$ A1 = array (0 => "Cat", 1 => "Dog", 2 => "Horse ");

$ A2 = array (3 => "Horse", 4 => "Dog", 5 => "Fish ");

Print_r (array_intersect ($ a1, $ a2 ));

?>

Output: Array ([1] => Dog [2] => Horse)

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 ...) Parameter description

Array1 is required. The first array to be compared with other arrays.

Array2 is required. The array to be compared with the first array.

Array3 is optional. The array to be compared with the first array.

Tips and comments

Tip: one or more Arrays can be compared with the first array.

Note: Only values are used for comparison.

Instance

  

$ A1 = array (0 => "Cat", 1 => "Dog", 2 => "Horse ");

$ A2 = array (3 => "Horse", 4 => "Dog", 5 => "Fish ");

Print_r (array_diff ($ a1, $ a2 ));

?>

Output: Array ([0] => Cat)

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.