在PHP中重置数组元素的键?

问题是如何重置键例如一个数组:

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 ) ) 

重置数组中所有数组的键:

 $arr = array_map('array_values', $arr); 

如果你只是想重置一级数组键,使用array_values()没有array_map

 $array[9] = 'Apple'; $array[12] = 'Orange'; $array[5] = 'Peach'; $array = array_values($array); 

通过这个function你可以重置你的数组

 $array[0] = 'Apple'; $array[1] = 'Orange'; $array[2] = 'Peach'; 

使用array_values重置键

 foreach($input as &$val) { $val = array_values($val); } 

http://php.net/array_values

在这里,您可以看到与简单的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 ) ) ) 
 $result = ['5' => 'cherry', '7' => 'apple']; array_multisort($result, SORT_ASC); print_r($result); 

Array([0] => apple [1] =>樱桃)

 //... array_multisort($result, SORT_DESC); //... 

Array([0] => cherry [1] => apple)

PHP本地函数为此存在。 见http://php.net/manual/en/function.reset.php

简单地做到这一点: mixed reset ( array &$array )