I have 1 array that I want to re-index. I have found that both array_values
and array_merge
functions can do the job (and I don't need 2 arrays for the array_merge
function to work).
我有1个数组,我想重新索引。我发现array_values和array_merge函数都可以完成这项工作(我不需要2个数组来使array_merge函数工作)。
Which is faster for a very large array? I would benchmark this, but I don't know how and don't have the large array yet.
对于一个非常大的阵列来说哪个更快?我会对此进行基准测试,但我不知道如何以及没有大阵列。
Before re-index:
在重新索引之前:
Array
(
[0] => AB
[4] => EA
[6] => FA
[9] => DA
[10] => AF
)
After re-index:
重新索引后:
Array
(
[0] => AB
[1] => EA
[2] => FA
[3] => DA
[4] => AF
)
4 个解决方案
#1
2
I haven't done the benchmarks either -- and if you need to be sure, you should do them.
我也没有做过基准测试 - 如果你需要确定,你应该这样做。
That said, I would suspect that if one is preferable to the other, array_values() is going to be the way to go.
也就是说,我怀疑如果一个比另一个更好,array_values()将是要走的路。
After all, what you want to do is exactly what array_values() was designed for.
毕竟,你想要做的就是为array_values()设计的。
#2
3
I got the bench mark, array_value is 3x faster (sorry to answer my own question, the comment section does not retain the format)
我得到了替补标记,array_value快了3倍(很遗憾回答我自己的问题,评论部分没有保留格式)
for and array with 8043 elements
for和8043元素的数组
array values took 0.003291130065918 seconds.
数组值花了0.003291130065918秒。
array merge took 0.0096800327301025 seconds.
阵列合并花了0.0096800327301025秒。
$shuf is the un-indexed array
$ shuf是未编入索引的数组
Below is the code for running the benchmark (copied it off the web)
以下是运行基准测试的代码(从网上复制)
$sha1_start = microtime(true);
$arraymerge = array_merge ($shuf);
$shal_elapsed = microtime(true) - $sha1_start;
$start = microtime(true);
$arrayvalue = array_values ($shuf);
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.";
echo "<br>array merge took $shal_elapsed seconds.";
#3
2
array_values is meant to do exactly what you want it to do. array_merge is meant to do something else and you got a workaround to make it work on your case. (although you might have problems if a non-numerical value is forgotten in the indexes).
array_values意味着完全按照您的意愿执行操作。 array_merge意味着要做其他事情,你有一个解决方法,使它适用于你的情况。 (尽管如果索引中忘记了非数值,您可能会遇到问题)。
I don't know if there are significant performance differences but for sure code written with array_values is easier to read. And I don't think that a function that is meant to do something is slower than one meant to do something else.
我不知道是否存在显着的性能差异,但确保使用array_values编写的代码更容易阅读。而且我认为用于做某事的函数比用于做其他事情的函数要慢。
Hope this helps.
希望这可以帮助。
#4
1
It's important to note that array_merge()
will only reset the array keys if there are no string keys in the array.
重要的是要注意,如果数组中没有字符串键,array_merge()将仅重置数组键。
#1
2
I haven't done the benchmarks either -- and if you need to be sure, you should do them.
我也没有做过基准测试 - 如果你需要确定,你应该这样做。
That said, I would suspect that if one is preferable to the other, array_values() is going to be the way to go.
也就是说,我怀疑如果一个比另一个更好,array_values()将是要走的路。
After all, what you want to do is exactly what array_values() was designed for.
毕竟,你想要做的就是为array_values()设计的。
#2
3
I got the bench mark, array_value is 3x faster (sorry to answer my own question, the comment section does not retain the format)
我得到了替补标记,array_value快了3倍(很遗憾回答我自己的问题,评论部分没有保留格式)
for and array with 8043 elements
for和8043元素的数组
array values took 0.003291130065918 seconds.
数组值花了0.003291130065918秒。
array merge took 0.0096800327301025 seconds.
阵列合并花了0.0096800327301025秒。
$shuf is the un-indexed array
$ shuf是未编入索引的数组
Below is the code for running the benchmark (copied it off the web)
以下是运行基准测试的代码(从网上复制)
$sha1_start = microtime(true);
$arraymerge = array_merge ($shuf);
$shal_elapsed = microtime(true) - $sha1_start;
$start = microtime(true);
$arrayvalue = array_values ($shuf);
$elapsed = microtime(true) - $start;
echo "<br>array values took $elapsed seconds.";
echo "<br>array merge took $shal_elapsed seconds.";
#3
2
array_values is meant to do exactly what you want it to do. array_merge is meant to do something else and you got a workaround to make it work on your case. (although you might have problems if a non-numerical value is forgotten in the indexes).
array_values意味着完全按照您的意愿执行操作。 array_merge意味着要做其他事情,你有一个解决方法,使它适用于你的情况。 (尽管如果索引中忘记了非数值,您可能会遇到问题)。
I don't know if there are significant performance differences but for sure code written with array_values is easier to read. And I don't think that a function that is meant to do something is slower than one meant to do something else.
我不知道是否存在显着的性能差异,但确保使用array_values编写的代码更容易阅读。而且我认为用于做某事的函数比用于做其他事情的函数要慢。
Hope this helps.
希望这可以帮助。
#4
1
It's important to note that array_merge()
will only reset the array keys if there are no string keys in the array.
重要的是要注意,如果数组中没有字符串键,array_merge()将仅重置数组键。