Filling an array with numbers sequentially

雨燕双飞 提交于 2019-12-19 15:57:32

问题


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

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

The only way I know to do this at the moment is by using a for loop, but I'm wondering if there's 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 for a set number of times.


回答1:


Use range:

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



回答2:


This is what you are looking for:

range(1, 6)


来源:https://stackoverflow.com/questions/9234572/filling-an-array-with-numbers-sequentially

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!