W3docs

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:

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:

How to reindex an array in PHP but with indexes starting from 1?

php— editable, runs on the server
Info

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',]