时间:2021-07-01 10:21:17 帮助过:6人阅读
代码如下:
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教程有兴趣的朋友有所帮助。