The efficiency of PHP's array_diff () function in processing large arrays. The code for copying the method submitted by cisa to the PHP official BUG page is as follows :? Php *** solution for php5.2.6 and later array_diff () functions to submit the cisa method to the PHP official BUG page when processing * large arrays that require a long time
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.
The code for the http://www.bkjia.com/PHPjc/324734.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324734.htmlTechArticlecisa to submit to the PHP official BUG page is as follows :? Php/*** solves the need for the array_diff () function of php 5.2.6 or later to process * large arrays...