Home >Backend Development >PHP Tutorial >How to Reindex a PHP Array Starting from 1?
Reindex an Array and Shift Indexes to Start from 1 in PHP
You have an array with keys starting from 2, and you want to reindex it with keys starting from 1.
To achieve this, you can use a combination of array functions in PHP:
$values = array_values($originalArray);
$reindexedArray = array_combine( range(1, count($values)), $values );
Output:
array ( [1] => Object ( [title] => Section [linked] => 1 ) [2] => Object ( [title] => Sub-Section [linked] => 1 ) [3] => Object ( [title] => Sub-Sub-Section [linked] => ) )
The above is the detailed content of How to Reindex a PHP Array Starting from 1?. For more information, please follow other related articles on the PHP Chinese website!