The array_diff () function of PHP5.2.6 and later versions takes a long time to process large arrays. this bug has been officially confirmed; before this problem is fixed or we cannot control the PHP version, you can use the method provided in this article to submit the cisa method to the PHP official BUG page.
The code is as follows:
/**
* The array_diff () function of php 5.2.6 or later is being processed.
* The long time required for large arrays
*
* Finishing: 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 );
}
?>
Rewrite method based on the idea of the ChinaUnix forum moderator hightman
The code is as follows:
/**
* The efficiency of array_diff () functions in php 5.2.6 and later versions in processing large arrays is solved.
* Method based on the idea of the ChinaUnix forum moderator hightman
*
* 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 ){
// Convert the key-value relationship of the second array
$ SecondArray = array_flip ($ secondArray );
// Loop the 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 exchanges the key and value of the second array, so it is more efficient.
Note: PHP's built-in array_diff () function can process multiple arrays. the methods provided in this article only process the comparison of two arrays.