Reset keys of array elements in php?

The question is how to make a reset key, for example. for 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 ) ) 
+70
php
May 8 '12 at 5:06
source share
6 answers

In reset, the keys of all arrays in the array:

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

If you want to simply reset the keys of a first level array, use array_values() without array_map .

+191
May 08 '12 at 5:09
source share
 $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'; 
+114
May 08 '12 at 5:11
source share

Use the array_values to reset keys

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

http://php.net/array_values

+10
May 08 '12 at 5:09 a.m.
source share

Here you can see the difference between what deceze suggested compared to the simple array_values approach:

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 , here is your conclusion:

 $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 conclusion, if you only use the 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 ) ) ) 
+4
Apr 05 '13 at 7:39
source share
 $result = ['5' => 'cherry', '7' => 'apple']; array_multisort($result, SORT_ASC); print_r($result); 

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

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

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

+2
Aug 19 '15 at 15:51
source share

There is a native PHP function for this. See http://php.net/manual/en/function.reset.php

Just do the following: mixed reset ( array &$array )

-7
Mar 29 '15 at 13:35
source share



All Articles