Efficiency issues of PHP's array_diff() function when processing large arrays_PHP Tutorial

WBOY
Release: 2016-07-21 15:22:22
Original
1034 people have browsed it

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 too long to process
* large arrays
*
* Organized: 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); > The code is as follows:


/**
* 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 the ChinaUnix forum
* * Organized: 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 second array The key-value relationship
$secondArray = array_flip($secondArray);
// Loop the first array
foreach($firstArray as $key => $value) {
// If the first array The value of the first array exists in the two arrays
if (isset($secondArray[$value])) {
// Remove the corresponding element in the first array
unset($firstArray[ $key]);
}
}
return $firstArray;
}
?>


This method only exchanges the keys of the second array 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.



http://www.bkjia.com/PHPjc/324734.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/324734.html
TechArticle

cisa Submit it to the PHP official BUG page. Copy the code as follows: ?php /** * Solve PHP 5.2 .6 and above, the array_diff() function takes a very long time when processing * large arrays...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!