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[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';
Use array_values
to reset keys
foreach($input as &$val) {
$val = array_values($val);
}
http://php.net/array_values
Here you can see the difference between the way that deceze offered comparing to the simple array_values
approach:
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 = 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
)
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With