Filling an array with numbers sequentially

I have a number (e.g. 6) that is dynamically generated, and I would like to populate the array with the numbers 1 through a dynamically generated number (in this example, 6):

array(1, 2, 3, 4, 5, 6); 

The only way I know this at the moment is to use a for loop, but I wonder if there is a better way, something similar to array_fill . I looked at array_fill, but it doesn't look like it will take a number and increment it a certain number of times.

+8
arrays increment php
source share
2 answers

Use range :

 $arr = range(1,6); // Returns an array of elements from start to limit, inclusive. 
+17
source share

This is what you are looking for:

 range(1, 6) 
+2
source share

All Articles