Efficiency of PHP array_diff () functions in processing large arrays _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.