Sometimes, we want to reindex an array in PHP but with indexes starting from 1.
In this article, we’ll look at how to reindex an array in PHP but with indexes starting from 1.
How to reindex an array in PHP but with indexes starting from 1?
To reindex an array in PHP but with indexes starting from 1, we use the array_combine
function.
For instance, we write
$iOne = array_combine(range(1, count($arr)), array_values($arr));
to call array_combine
with an array of indexes and the values of the $arr
array that we get from array_values
.
Then we get an array with entries of $arr
starting with index 1 instead of 0.
Conclusion
To reindex an array in PHP but with indexes starting from 1, we use the array_combine
function.