/ **
* Solve the problem that the array_diff() function in PHP 5.2.6 and above takes too long to process
* large arrays
*
* Organized: http://www.CodeBit. cn
* Source: http://bugs.php.net/47643
*/
function array_diff_fast($data1, $data2) {
$data1 = array_flip($data1);
$data2 = array_flip($data2);
foreach($ data2 as $hash => $key) {
if (isset($data1[$hash])) unset($data1[$hash]);
}
return array_flip($data1); > The code is as follows:
/**
* Solve the efficiency problem of the array_diff() function in PHP 5.2.6 and above when processing large arrays
* The method written according to the idea of hightman, the moderator of the ChinaUnix forum
* * Organized: 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 second array The key-value relationship
$secondArray = array_flip($secondArray);
// Loop the first array
foreach($firstArray as $key => $value) {
// If the first array The value of the first array exists in the two arrays
if (isset($secondArray[$value])) {
// Remove the corresponding element in the first array
unset($firstArray[ $key]);
}
}
return $firstArray;
}
?>
This method only exchanges the keys of the second array and value, so it is more efficient.
Note: PHP’s built-in array_diff() function can handle multiple arrays, but the method provided in this article only handles the comparison of two arrays.
http://www.bkjia.com/PHPjc/324734.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/324734.html
TechArticle
cisa Submit it to the PHP official BUG page. Copy the code as follows: ?php /** * Solve PHP 5.2 .6 and above, the array_diff() function takes a very long time when processing * large arrays...