PHP array_intersect faster than Array_diff (with detailed instructions) _php tips

Source: Internet
Author: User
If you want the array to $a the number of difference sets $b to the array, you should use COUNT ($a)-count (Array_intersect ($a, $b) instead of Count (Array_diff ($a, $b));

The front is faster than the latter, and is more pronounced in large arrays.

1.array_intersect function
Array Array_intersect (array $array 1, array $array 2 [, array $ ...])
Array_intersect () returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note that the key name remains unchanged.
#1 Array_intersect () example
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "Red", "blue");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
?>
This makes $result become:
Array
(
[A] => green
[0] => Red
)

2. Self-Implementation array_intersect () function is five times times faster than PHP original function array_intersect ()
Copy Code code as follows:

/**
*
* Custom Array_intersect
* If we ask for the intersection of one-dimensional arrays, this function is 5 times times faster than the array_intersect of the system.
*
* @param array $arr 1
* @param array $arr 2
* @author Liubotao 2010-12-13 11:40:20
*
*/
function My_array_intersect ($arr 1, $arr 2)
{
For ($i =0 $i <sizeof ($arr 1); $i + +)
{
$temp []= $arr 1[$i];
}
For ($i =0 $i <sizeof ($arr 1); $i + +)
{
$temp []= $arr 2[$i];
}
Sort ($temp);
$get =array ();
for ($i =0; $i <sizeof ($temp); $i + +)
{
if ($temp [$i]== $temp [$i +1])
$get []= $temp [$i];
}
return $get;
}
$array 1 = Array ("Green", "Red", "blue");
$array 2 = Array ("green", "yellow", "red");
echo "<pre>";
Print_r (My_array_intersect ($array 1, $array 2));
echo "<pre/>";

array_diff-Calculating the difference set of an array

Array Array_diff (array $array 1, array $array 2 [, array $ ...])
Array_diff () returns an array that includes all values that are in array1 but not in any other parameter array. Note that the key name remains unchanged.

#1 Array_diff () example
Copy Code code as follows:

<?php
$array 1 = Array ("A" => "green", "Red", "Blue", "Red");
$array 2 = Array ("B" => "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);
Print_r ($result);
?>

Processed in the same way as a value that occurs more than once in $array 1, the output is:
Copy Code code as follows:

Array
(
[1] => Blue
)

Note: Two units are considered identical only in (string) $elem 1 = = (string) $elem 2 o'clock. In other words, when the string expression is the same.

Note: Notice that this function examines only one dimension of a multidimensional array. Certainly can use Array_diff ($array 1[0], $array 2[0]); Check for 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.