依次用数字填充数组 [英] Filling an array with numbers sequentially

查看:108
本文介绍了依次用数字填充数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态生成的数字(例如6),我想通过动态生成的数字(在本例中为6)用数字1填充数组:

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);

我目前唯一知道的方法是使用for循环,但是我想知道是否还有更好的方法,类似于array_fill.我看了一下array_fill,但看起来并不需要一个数字并将其递增设定的次数.

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.

推荐答案

使用范围:

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

这篇关于依次用数字填充数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆