CISA the way to submit to the official PHP BUG page
Copy Code code as follows:
<?php
/**
* Solve PHP 5.2.6 version Array_diff () function in processing
* Large array of problems that take an extra long time
*
* Finishing: http://www.CodeBit.cn
* Source: http://bugs.php.net/47643
*/
function Array_diff_fast ($data 1, $data 2) {
$data 1 = array_flip ($data 1);
$data 2 = Array_flip ($data 2);
foreach ($data 2 as $hash => $key) {
if (Isset ($data 1[$hash])) unset ($data 1[$hash]);
}
Return Array_flip ($data 1);
}
?>
According to the Chinaunix forum moderator Hightman Ideas rewrite method
Copy Code code as follows:
<?php
/**
* Solve the efficiency problem of PHP 5.2.6 version Array_diff () function in handling large arrays
* According to the Chinaunix forum moderator Hightman Ideas to write the method
*
* Finishing: http://www.CodeBit.cn
* Reference: http://bbs.chinaunix.net/viewthread.php?tid=938096&rpid=6817036&ordertype=0&page=1#pid6817036
*/
function Array_diff_fast ($firstArray, $secondArray) {
Converts a key value relationship for a second array
$secondArray = Array_flip ($secondArray);
Loop First Array
foreach ($firstArray as $key => $value) {
If the value of the first array exists in the second array
if (Isset ($secondArray [$value])) {
Remove the corresponding element from the first array
Unset ($firstArray [$key]);
}
}
return $firstArray;
}
?>
This method only swaps the key and value of the second array, so it is more efficient.
Note: PHP's built-in Array_diff () function can handle multiple arrays, and the method provided in this article deals with comparisons of only two arrays.