How to re-index all subarray elements of a multidimensional array?

Leo Chan picture Leo Chan · May 8, 2012 · Viewed 208.2k times · Source

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

Answer

deceze picture deceze · May 8, 2012

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.