重置 PHP 数组索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7536961/
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
Reset PHP Array Index
提问by Walter Johnson
I have a PHP array that looks like this:
我有一个看起来像这样的 PHP 数组:
[3] => Hello
[7] => Moo
[45] => America
What PHP function makes this?
什么 PHP 函数使这个?
[0] => Hello
[1] => Moo
[2] => America
回答by Facebook Staff are Complicit
回答by Allan Mwesigwa
If you want to reset the key countof the array for some reason;
如果由于某种原因要重置数组的键计数;
$array1 = [
[3] => 'Hello',
[7] => 'Moo',
[45] => 'America'
];
$array1 = array_merge($array1);
print_r($array1);
Output:
输出:
Array(
[0] => 'Hello',
[1] => 'Moo',
[2] => 'America'
)
回答by Gufran Hasan
Use array_keys()function get keys of an array and array_values()function to get values of an array.
使用array_keys()函数获取数组的键和array_values()函数获取数组的值。
You want to get values of an array:
您想获取数组的值:
$array = array( 3 => "Hello", 7 => "Moo", 45 => "America" );
$arrayValues = array_values($array);// returns all values with indexes
echo '<pre>';
print_r($arrayValues);
echo '</pre>';
Output:
输出:
Array
(
[0] => Hello
[1] => Moo
[2] => America
)
You want to get keys of an array:
您想获取数组的键:
$arrayKeys = array_keys($array);// returns all keys with indexes
echo '<pre>';
print_r($arrayKeys);
echo '</pre>';
Output:
输出:
Array
(
[0] => 3
[1] => 7
[2] => 45
)