PHP 5.2. More than 6 version of the Array_diff () function takes a long time to handle a large array, the bug has been officially confirmed, and the method provided in this article can be used before the problem is fixed or when we cannot control the PHP version
Cisa a method to submit to the official PHP BUG page
The code is as follows:
<?php/** * resolves php 5.2.6 or later versions of the Array_diff () function that takes an extra long time to process a large array * * Collation: http://www. codebit.cn * Source: http://bugs.php.net/47643 */Function Array_diff_fast ($data 1, $data 2) {$data 1 = array_flip ($data 1); $dat A2 = 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 Chinaunix forum moderator Hightman Ideas to rewrite the method
The code is as follows:
<?php/** * Solve PHP 5.2.6 or later version of the Array_diff () function when working with large arrays * According to the Chinaunix forum moderator Hightman Ideas written method * Collation: 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) {//Convert the key value relationship of the second array $secondArray = Array_flip ($ Secondarray); Loop the first array of foreach ($firstArray as $key + $value) {//If the value of the first array exists in the second array if (Isset ($secondArray [$value])) {//Removes the first array The corresponding element 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 only handles comparisons of two arrays.