Home > Article > Backend Development > PHP method to restore the key of an array to a numerical sequence, array key_PHP tutorial
The example in this article describes how PHP restores the key of an array to a sequence of numbers. Share it with everyone for your reference. The specific analysis is as follows:
Here, PHP is implemented to restore the key value of the array into a sequence of numbers similar to 0,1,2,3,4,5...
function restore_array($arr){ if (!is_array($arr)){ return $arr; } $c = 0; $new = array(); while (list($key, $value) = each($arr)){ if (is_array($value)){ $new[$c] = restore_array($value); } else { $new[$c] = $value; } $c++; } return $new; }
Demo example:
Copy code The code is as follows: restore_array(array('a' => 1, 'b' => 2)); --> returns array(0 => 1, 1 => 2)
I hope this article will be helpful to everyone’s PHP programming design.