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:


$array = ['a', 'b', 'c'];
$newArray = array_combine(range(1, count($array)), array_values($array));


Watch a course Learn object oriented PHP

This will create a new array $newArray with the following structure:

[    1 => 'a',    2 => 'b',    3 => 'c',]