Home  >  Article  >  Backend Development  >  serializearray PHP's array_diff function efficiency issues when processing large arrays

serializearray PHP's array_diff function efficiency issues when processing large arrays

WBOY
WBOYOriginal
2016-07-29 08:47:241055browse

The method for cisa to submit to the PHP official BUG page

Copy the code The code is as follows:


/**
* Solve the problem that the array_diff() function in PHP 5.2.6 and above takes a long time when processing
* large arrays
*
* Compilation: 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);
}
?> /**

* 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 ChinaUnix Forum
*

* Organized by: 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 in the first array
unset($firstArray[$key]);
}
}
return $firstArray;
}
?>


This method only exchanges the second array key 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.
The above introduces the efficiency issues of serializearray PHP's array_diff function when processing large arrays, including the content of serializearray. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn