The question is how to reset key e.g. for an array:
问题是如何重置键,例如数组:
Array (
[1_Name] => Array (
[1] => leo
[4] => NULL
)
[1_Phone] => Array (
[1] => 12345
[4] => 434324
)
)
reset to :
重置:
Array (
[1_Name] => Array (
[0] => leo
[1] => NULL
)
[1_Phone] => Array (
[0] => 12345
[1] => 434324
)
)
6 个解决方案
#1
215
To reset the keys of all arrays in an array:
重置数组中所有数组的键:
$arr = array_map('array_values', $arr);
In case you just want to reset first-level array keys, use array_values()
without array_map
.
如果您只想重置一级数组的键,使用array_values(),而不需要array_map。
#2
129
$array[9] = 'Apple';
$array[12] = 'Orange';
$array[5] = 'Peach';
$array = array_values($array);
through this function you can reset your array
通过这个函数可以重置数组
$array[0] = 'Apple';
$array[1] = 'Orange';
$array[2] = 'Peach';
#3
13
Use array_values
to reset keys
使用array_values重置键
foreach($input as &$val) {
$val = array_values($val);
}
http://php.net/array_values
#4
6
Here you can see the difference between the way that deceze offered comparing to the simple array_values
approach:
在这里你可以看到,与简单的array_values方法相比,frauze提供的方法是不同的:
The Array:
数组:
$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);
In deceze
way, here is your output:
以下是你的输出:
$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
)
)
)
And here is your output if you only use array_values
function:
这是你的输出如果你只使用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
)
)
)
#5
2
$result = ['5' => 'cherry', '7' => 'apple'];
array_multisort($result, SORT_ASC);
print_r($result);
Array ( [0] => apple [1] => cherry )
数组([0]=> apple [1] => cherry)
//...
array_multisort($result, SORT_DESC);
//...
Array ( [0] => cherry [1] => apple )
阵列([0]=> cherry[1] =>苹果)
#6
-7
PHP native function exists for this. See http://php.net/manual/en/function.reset.php
为此存在PHP本机函数。参见http://php.net/manual/en/function.reset.php
Simply do this: mixed reset ( array &$array )
只需这样做:混合重置(数组&$数组)
#1
215
To reset the keys of all arrays in an array:
重置数组中所有数组的键:
$arr = array_map('array_values', $arr);
In case you just want to reset first-level array keys, use array_values()
without array_map
.
如果您只想重置一级数组的键,使用array_values(),而不需要array_map。
#2
129
$array[9] = 'Apple';
$array[12] = 'Orange';
$array[5] = 'Peach';
$array = array_values($array);
through this function you can reset your array
通过这个函数可以重置数组
$array[0] = 'Apple';
$array[1] = 'Orange';
$array[2] = 'Peach';
#3
13
Use array_values
to reset keys
使用array_values重置键
foreach($input as &$val) {
$val = array_values($val);
}
http://php.net/array_values
#4
6
Here you can see the difference between the way that deceze offered comparing to the simple array_values
approach:
在这里你可以看到,与简单的array_values方法相比,frauze提供的方法是不同的:
The Array:
数组:
$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);
In deceze
way, here is your output:
以下是你的输出:
$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
)
)
)
And here is your output if you only use array_values
function:
这是你的输出如果你只使用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
)
)
)
#5
2
$result = ['5' => 'cherry', '7' => 'apple'];
array_multisort($result, SORT_ASC);
print_r($result);
Array ( [0] => apple [1] => cherry )
数组([0]=> apple [1] => cherry)
//...
array_multisort($result, SORT_DESC);
//...
Array ( [0] => cherry [1] => apple )
阵列([0]=> cherry[1] =>苹果)
#6
-7
PHP native function exists for this. See http://php.net/manual/en/function.reset.php
为此存在PHP本机函数。参见http://php.net/manual/en/function.reset.php
Simply do this: mixed reset ( array &$array )
只需这样做:混合重置(数组&$数组)