方式一(循环中使用array_merge)
function eachOne(int $times): array
{
$a = [];
$b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for ($i = 0; $i < $times; $i++) {
$a = array_merge($a, $b);
}
return $a;
}
方式二(循环后使用array_merg合并)
function eachTwo(int $times): array
{
$a = [[]];
$b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for ($i = 0; $i < $times; $i++) {
$a[] = $b;
}
return array_merge(...$a);
}
速度对比
eachOne(10000);
eachTwo(10000);
通过实验对比,方式一耗时7秒,而方式二仅0.002秒。
试验方法:https://blog.csdn.net/weixin_33712881/article/details/91380140