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
$array = ['a', 'b', 'c'];
$newArray = array_combine(range(1, count($array)), array_values($array));
print_r($newArray);
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
This will create a new array $newArray with the following structure:
[ 1 => 'a', 2 => 'b', 3 => 'c',]