我有一个号码,让我们说它现在是5.我想要的是获得一个从0到5的数组.
举个例子:
$input = 5;
// Do something
$output = array(0,1,2,3,4,5);
Run Code Online (Sandbox Code Playgroud)
我做了这样的事情:
$i = 0;
$input = 5;
$output = array();
while($i <= $input) {
$output[] = $i;
$i++;
}
Run Code Online (Sandbox Code Playgroud)
快速print_r($output);将导致:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
Run Code Online (Sandbox Code Playgroud)
看起来不错,但我希望有一个更小更快的解决方案.有什么建议?他们是PHP函数我缺少/不知道吗?
也许range()?
$output = range(0, $input);
print_r($output);
Run Code Online (Sandbox Code Playgroud)
输出:
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
)
Run Code Online (Sandbox Code Playgroud)