array_merge 与 array_value 用于重置数组索引

发布于 2024-09-03 17:03:17 字数 624 浏览 8 评论 0原文

我有 1 个数组想要重新索引。我发现 array_valuesarray_merge 函数可以完成这项工作(而且我不需要2 个数组供 array_merge 函数使用)。

对于非常大的数组来说哪个更快?我会对此进行基准测试,但我不知道如何进行,而且还没有大数组。

重新索引之前:

Array
(
    [0] => AB
    [4] => EA
    [6] => FA
    [9] => DA
    [10] => AF
)

重新索引之后:

Array
(
    [0] => AB
    [1] => EA
    [2] => FA
    [3] => DA
    [4] => AF
)

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).

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
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风尘浪孓 2024-09-10 17:03:17

我得到了基准,array_value 的速度快了 3 倍(抱歉回答我自己的问题,注释部分不保留格式),

并且具有 8043 个元素的

数组值的数组花费了 0.003291130065918 秒。

数组合并花费了 0.0096800327301025 秒。

$shuf 是未索引的数组

下面是运行基准测试的代码(从网上复制的)

    $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.";

I got the bench mark, array_value is 3x faster (sorry to answer my own question, the comment section does not retain the format)

for and array with 8043 elements

array values took 0.003291130065918 seconds.

array merge took 0.0096800327301025 seconds.

$shuf is the un-indexed array

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.";
香草可樂 2024-09-10 17:03:17

我也没有做过基准测试——如果你需要确定,你应该做它们。

也就是说,我怀疑如果一个比另一个更可取,则 array_values() 将是最佳选择。

毕竟,您想要做的正是 array_values() 的设计目的。

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.

After all, what you want to do is exactly what array_values() was designed for.

暖风昔人 2024-09-10 17:03:17

array_values 旨在完全按照您想要的方式执行。
array_merge 的目的是做其他事情,并且您有一个解决方法可以使其适用于您的情况。 (尽管如果索引中忘记了非数字值,您可能会遇到问题)。

我不知道是否存在显着的性能差异,但可以肯定的是,使用 array_values 编写的代码更易于阅读。我不认为一个用于做某事的函数比用于做其他事情的函数慢。

希望这有帮助。

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).

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.

Hope this helps.

天涯离梦残月幽梦 2024-09-10 17:03:17

需要注意的是,如果数组中没有字符串键,array_merge() 只会重置数组键。

It's important to note that array_merge() will only reset the array keys if there are no string keys in the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文