How do you reindex an array in PHP but with indexes starting from 1?
You can use the array_values
function to get a new array with the values of the original array, and then use the array_combine
function to create an array using the new values and an indexed array of keys starting from 1:
<?php
$array = ['a', 'b', 'c'];
$newArray = array_combine(range(1, count($array)), array_values($array));
print_r($newArray);
Watch a video course
Learn object oriented PHP
This will create a new array $newArray
with the following structure:
[ 1 => 'a', 2 => 'b', 3 => 'c',]