php PHP重新索引数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7558022/
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
PHP reindex array?
提问by MrWhddite333
I have array that i had to unset some indexes so now it looks like
我有一个数组,我不得不取消一些索引,所以现在看起来像
$myarray [0] a->1
[1] a-7 b->3
[3] a-8 b->6
[4] a-3 b->2
as you can see [2] is missing all i need to do is reset indexes so they show [0]-[3].
如您所见,[2] 缺少我需要做的就是重置索引,以便它们显示 [0]-[3]。
回答by Alex Turpin
回答by Alfwed
回答by Drasill
array_values does the job :
array_values 做的工作:
$myArray = array_values($myArray);
Also some other php function do not preserve the keys, i.e. reset the index.
还有一些其他的 php 函数不保留键,即重置索引。
回答by krishna
This might not be the simplest answer as compared to using array_values().
与使用 array_values() 相比,这可能不是最简单的答案。
Try this
尝试这个
$array = array( 0 => 'string1', 2 => 'string2', 4 => 'string3', 5 => 'string4');
$arrays =$array;
print_r($array);
$array=array();
$i=0;
foreach($arrays as $k => $item)
{
$array[$i]=$item;
unset($arrays[$k]);
$i++;
}
print_r($array);