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

怪我咯
Release: 2023-03-12 18:58:02
Original
1476 people have browsed it

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;
}
Copy after login

Demonstration example:

The code is as follows:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!