问题是如何重置密钥,例如对于数组:
Array (
[1_Name] => Array (
[1] => leo
[4] => NULL
)
[1_Phone] => Array (
[1] => 12345
[4] => 434324
)
)
重置为:
Array (
[1_Name] => Array (
[0] => leo
[1] => NULL
)
[1_Phone] => Array (
[0] => 12345
[1] => 434324
)
)
答案 0 :(得分:249)
答案 1 :(得分:154)
$array[9] = 'Apple';
$array[12] = 'Orange';
$array[5] = 'Peach';
$array = array_values($array);
通过此功能,您可以重置阵列
$array[0] = 'Apple';
$array[1] = 'Orange';
$array[2] = 'Peach';
答案 2 :(得分:13)
使用array_values
重置密钥
foreach($input as &$val) {
$val = array_values($val);
}
答案 3 :(得分:6)
在这里,您可以看到deceze提供的方式与简单array_values
方法之间的差异:
数组:
$array['a'][0] = array('x' => 1, 'y' => 2, 'z' => 3);
$array['a'][5] = array('x' => 4, 'y' => 5, 'z' => 6);
$array['b'][1] = array('x' => 7, 'y' => 8, 'z' => 9);
$array['b'][7] = array('x' => 10, 'y' => 11, 'z' => 12);
以deceze
方式,这是您的输出:
$array = array_map('array_values', $array);
print_r($array);
/* Output */
Array
(
[a] => Array
(
[0] => Array
(
[x] => 1
[y] => 2
[z] => 3
)
[1] => Array
(
[x] => 4
[y] => 5
[z] => 6
)
)
[b] => Array
(
[0] => Array
(
[x] => 7
[y] => 8
[z] => 9
)
[1] => Array
(
[x] => 10
[y] => 11
[z] => 12
)
)
)
如果您只使用array_values
函数,那么这是您的输出:
$array = array_values($array);
print_r($array);
/* Output */
Array
(
[0] => Array
(
[0] => Array
(
[x] => 1
[y] => 2
[z] => 3
)
[5] => Array
(
[x] => 4
[y] => 5
[z] => 6
)
)
[1] => Array
(
[1] => Array
(
[x] => 7
[y] => 8
[z] => 9
)
[7] => Array
(
[x] => 10
[y] => 11
[z] => 12
)
)
)
答案 4 :(得分:3)
$result = ['5' => 'cherry', '7' => 'apple'];
array_multisort($result, SORT_ASC);
print_r($result);
数组([0] => apple [1] =>樱桃)
//...
array_multisort($result, SORT_DESC);
//...
数组([0] => cherry [1] => apple)
答案 5 :(得分:-8)
存在PHP本机函数。见http://php.net/manual/en/function.reset.php
只需执行此操作:mixed reset ( array &$array )