Skip to content
Advertisement

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

I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1):

Current array (edit: the array actually looks like this):

JavaScript

How it should be:

JavaScript

Advertisement

Answer

If you want to re-index starting to zero, simply do the following:

JavaScript

If you need it to start at one, then use the following:

JavaScript

Here are the manual pages for the functions used:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement