Example:
例子:
$arr = array(1 => 'Foo', 5 => 'Bar', 6 => 'Foobar');
/*... do some function so $arr now equals:
array(0 => 'Foo', 1 => 'Bar', 2 => 'Foobar');
*/
4 个解决方案
#1
14
Use array_values($arr)
. That will return a regular array of all the values (indexed numerically).
使用元素(arr)美元。这将返回所有值的常规数组(按数字索引)。
PHP文档元素
#2
4
array_values($arr);
#3
1
To add to the other answers, array_values()
will not preserve string keys. If your array has a mix of string keys and numeric keys (which is probably an indication of bad design, but may happen nonetheless), you can use a function like:
要添加到其他答案,array_values()不会保存字符串键。如果您的数组包含字符串键和数字键(这可能是设计不好的迹象,但也可能发生),您可以使用如下函数:
function reset_numeric_keys($array = array(), $recurse = false) {
$returnArray = array();
foreach($array as $key => $value) {
if($recurse && is_array($value)) {
$value = reset_numeric_keys($value, true);
}
if(gettype($key) == 'integer') {
$returnArray[] = $value;
} else {
$returnArray[$key] = $value;
}
}
return $returnArray;
}
#4
-1
Not that I know of, you might have already checked functions here
我不知道,你们可能已经检查过函数了
but I can imagine writing a simple function myself
但我可以想象自己写一个简单的函数
resetarray($oldarray)
{
for(int $i=0;$i<$oldarray.count;$i++)
$newarray.push(i,$oldarray[i])
return $newarray;
}
I am little edgy on syntax but I guess u got the idea.
我对语法有点紧张,但我猜你有这个想法。
#1
14
Use array_values($arr)
. That will return a regular array of all the values (indexed numerically).
使用元素(arr)美元。这将返回所有值的常规数组(按数字索引)。
PHP文档元素
#2
4
array_values($arr);
#3
1
To add to the other answers, array_values()
will not preserve string keys. If your array has a mix of string keys and numeric keys (which is probably an indication of bad design, but may happen nonetheless), you can use a function like:
要添加到其他答案,array_values()不会保存字符串键。如果您的数组包含字符串键和数字键(这可能是设计不好的迹象,但也可能发生),您可以使用如下函数:
function reset_numeric_keys($array = array(), $recurse = false) {
$returnArray = array();
foreach($array as $key => $value) {
if($recurse && is_array($value)) {
$value = reset_numeric_keys($value, true);
}
if(gettype($key) == 'integer') {
$returnArray[] = $value;
} else {
$returnArray[$key] = $value;
}
}
return $returnArray;
}
#4
-1
Not that I know of, you might have already checked functions here
我不知道,你们可能已经检查过函数了
but I can imagine writing a simple function myself
但我可以想象自己写一个简单的函数
resetarray($oldarray)
{
for(int $i=0;$i<$oldarray.count;$i++)
$newarray.push(i,$oldarray[i])
return $newarray;
}
I am little edgy on syntax but I guess u got the idea.
我对语法有点紧张,但我猜你有这个想法。