Home  >  Article  >  Backend Development  >  How to restore the key of the array to a numerical sequence in PHP

How to restore the key of the array to a numerical sequence in PHP

怪我咯
怪我咯Original
2017-07-05 10:49:211524browse

This article mainly introduces the method of PHP to restore the key of array to a numerical sequence, involving the skills of PHP operating array key names. Friends who need it can refer to the example of

PHP method to restore 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 to 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;
}

Demonstration example:

The code is as follows:

restore_array(array('a' => 1, 'b' => 2)); --> returns array(0 => 1, 1 => 2)

The above is the detailed content of How to restore the key of the array to a numerical sequence in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn