php 重新索引数字数组键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10524156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:23:49  来源:igfitidea点击:

Re-index numeric array keys

phparraysindexing

提问by Tower

I have an array that is built using the explode()function, but seeing how i'm using it with random/dynamic data, i see that the indexes keep changing:

我有一个使用该explode()函数构建的数组,但是看到我如何将它与随机/动态数据一起使用,我看到索引不断变化:

Array
(
    [2] => Title: Warmly little before cousin sussex entire set Blessing it ladyship.
    [3] => Snippet: Testing
    [4] => Category: Member
    [5] => Tags: little, before, entire
)

I need the array to be ordered starting at 0 always. I am testing with different data and sometimes it starts at 0, and with other tests it begins at different numbers. I researched and came across Array starting at zerobut it seems that only applied to that users specific case. The code i'm using to build the array can be seen here: https://stackoverflow.com/a/10484967/1183323

我需要数组总是从 0 开始排序。我正在用不同的数据进行测试,有时它从 0 开始,而在其他测试中,它从不同的数字开始。我研究并发现Array 从零开始,但似乎只适用于该用户的特定情况。我用来构建数组的代码可以在这里看到:https: //stackoverflow.com/a/10484967/1183323

How can i do this?

我怎样才能做到这一点?

回答by J. Bruni

$your_new_array = array_values($your_old_array);

回答by Keith Palmer Jr.

Use array_merge()to renumber the array:

使用array_merge()对数组重新编号:

$your_old_array = array( 2 => 'whatever', 19 => 'huh', 22 => 'yep' );
$your_new_array = array_merge($your_old_array);
print_r($your_new_array);

Prints this:

打印这个:

Array ( 
  [0] => whatever 
  [1] => huh 
  [2] => yep )