Php array_intersect is faster than array_diff (with detailed instructions)

Source: Internet
Author: User

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.

1. array_intersect Function
Array array_intersect (array $ array1, array $ array2 [, array $...])
Array_intersect () returns an array containing all values that appear in the array of all other parameters at the same time in array1. Note that the key name remains unchanged.
#1 array_intersect () Example
Copy codeThe Code is as follows:
<? Php
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2 );
?>
This makes $ result:
Array
(
[A] => green
[0] => red
)

2. Self-implemented array_intersect () functions are five times faster than the original php function array_intersect ().
Copy codeThe Code is as follows:
/**
*
* Custom array_intersect
* If the result is the intersection of one-dimensional arrays, this function is five times faster than the system's array_intersect.
*
* @ Param array $ arr1
* @ Param array $ arr2
* @ Author LIUBOTAO 2010-12-13 11:40:20 AM
*
*/
Function my_array_intersect ($ arr1, $ arr2)
{
For ($ I = 0; $ I <sizeof ($ arr1); $ I ++)
{
$ Temp [] = $ arr1 [$ I];
}
For ($ I = 0; $ I <sizeof ($ arr1); $ I ++)
{
$ Temp [] = $ arr2 [$ I];
}
Sort ($ temp );
$ Get = array ();
For ($ I = 0; $ I <sizeof ($ temp); $ I ++)
{
If ($ temp [$ I] ==$ temp [$ I + 1])
$ Get [] = $ temp [$ I];
}
Return $ get;
}
$ Array1 = array ("green", "red", "blue ");
$ Array2 = array ("green", "yellow", "red ");
Echo "<pre> ";
Print_r (my_array_intersect ($ array1, $ array2 ));
Echo "<pre/> ";

Array_diff-calculate the difference set of the array

Array array_diff (array $ array1, array $ array2 [, array $...])
Array_diff () returns an array containing all values in the array of array1 but not in any other parameter. Note that the key name remains unchanged.

#1 array_diff () Example
Copy codeThe Code is as follows:
<? Php
$ Array1 = array ("a" => "green", "red", "blue", "red ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );
Print_r ($ result );
?>

The output result is as follows:
Copy codeThe Code is as follows:
Array
(
[1] => blue
)

Note: The two units are considered the same only when (string) $ elem1 === (string) $ elem2. That is to say, when the expression of the string is the same.

Note: This function only checks one dimension in a multi-dimensional array. Of course, you can use array_diff ($ array1 [0], $ array2 [0]); to check deeper dimensions.

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.