Serializearray the efficiency of PHP's Array_diff function in handling large arrays

Source: Internet
Author: User
Cisa a method to submit to the official PHP BUG page

Copy the Code code as follows:


/**
* Fix PHP 5.2.6 or later Array_diff () function in processing
* Large array of problems that take an extra long time
*
* Finishing: http://www.CodeBit.cn
* Source: http://bugs.php.net/47643
*/
function Array_diff_fast ($data 1, $data 2) {
$data 1 = array_flip ($data 1);
$data 2 = 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

Copy the Code code as follows:


/**
* Resolve PHP 5.2.6 or later the efficiency of the Array_diff () function when dealing with large arrays
* According to Chinaunix forum moderator Hightman Thinking method of writing
*
* 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) {
Converts the key-value relationship of the second array
$secondArray = Array_flip ($secondArray);
Loop First Array
foreach ($firstArray as $key = = $value) {
If there is a value for the first array in the second array
if (Isset ($secondArray [$value])) {
Removes the corresponding element from the first array
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.

The above describes the Serializearray php Array_diff function in the handling of large arrays of efficiency, including the serializearray aspect of the content, I hope that the PHP tutorial interested in a friend to help.

  • Related Article

    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.