Home  >  Article  >  Backend Development  >  PHP method to restore the key of an array to a numerical sequence, array key_PHP tutorial

PHP method to restore the key of an array to a numerical sequence, array key_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:31892browse

How PHP restores the key of an array to a sequence of numbers, array key

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/991805.htmlTechArticlephp method to restore the key of an array to a sequence of numbers, array key This article describes how to restore the key of an array to a number in php sequence method. Share it with everyone for your reference. The specific analysis is as follows:...
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