function array_diff($array_1, $array_2) {
$diff = array(); foreach ($array_1 as $k => $v1) {
$flag = false; foreach ($array_2 as $v2) {
if ($flag = ($v1 == $v2)) {
k] = $v1;
}Although the implementation is possible, I found that the efficiency of this function is appalling. So I reconsidered and optimized the algorithm. The second function looked like this:
function array_diff($array_1, $array_2) {
foreach ($array_1 as $key => $item) {
The speed of the original array_diff function is comparable to . But is there a more optimized way? From an article on ChinaUnix (sorry, I cheated), I found that PHP can actually write like this:
function array_diff($array_1, $array_2) {
$array_2 = array_flip($array_2);
foreach ($array_1 as $key => $item) {
}This function is very efficient It's amazing, even faster than the original array_diff function. Investigating the reason, I found an explanation:
Because the key is organized by HASH, the search is very fast;
The Value is only stored by the Key organization and has no index itself, so every search is traversed. Summary
Although this is a little trick of the PHP language, when traversing and comparing array values, if you need to compare the value, reversing it with the key is indeed much more efficient than the usual value-to-value comparison.
For example, function two above needs to call the in_array function and needs to loop to determine whether it is within the function; while function three only determines whether the key exists in the array. Coupled with the different organizational indexing methods of array keys and values, it is very understandable that the efficiency is higher than imagined.
Attached code
Copy the code
The code is as follows:
function microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function array_diff2($array_1, $array_2) {
$diff = array();
foreach ($array_1 as $k => $v1) {
$flag = false;
foreach ($array_2 as $v2) {
if ($flag = ($v1 == $v2)) {
break;
}
}
if (!$flag) {
$diff[$k] = $v1;
}
}
return $diff;
}
function array_diff3($array_1, $array_2) {
foreach ($array_1 as $key => $item) {
if (in_array($item, $array_2, true)) {
unset($array_1[$key]);
}
}
return $array_1;
}
function array_diff4($array_1, $array_2) {
$array_2 = array_flip($array_2);
foreach ($array_1 as $key => $item) {
if (isset($array_2[$item])) {
unset($array_1[$key]);
}
}
return $array_1;
}
//////////////////////////////
for($i = 0, $ary_1 = array(); $i < 5000; $i++) {
$ary_1[] = rand(100, 999);
}
for($i = 0, $ary_2 = array(); $i < 5000; $i++) {
$ary_2[] = rand(100, 999);
}
header("Content-type: text/plain;charset=utf-8");
$time_start = microtime_float();
array_diff($ary_1, $ary_2);
echo "函数 array_diff 运行" . (microtime_float() - $time_start) . " 秒n";
$time_start = microtime_float();
array_diff2($ary_1, $ary_2);
echo "函数 array_diff2 运行" . (microtime_float() - $time_start) . " 秒n";
$time_start = microtime_float();
array_diff3($ary_1, $ary_2);
echo "函数 array_diff3 运行" . (microtime_float() - $time_start) . " 秒n";
$time_start = microtime_float();
array_diff4($ary_1, $ary_2);
echo "函数 array_diff4 运行" . (microtime_float() - $time_start) . " 秒n";
?>
以上就介绍了colesafearray 深思 PHP 数组遍历的差异(array_diff 的实现),包括了colesafearray方面的内容,希望对PHP教程有兴趣的朋友有所帮助。