开发者

PHP: Create an array for a range

开发者 https://www.devze.com 2022-12-11 22:21 出处:网络
If I have a variable $num = 50 how can I put the numbers 1-开发者_Go百科50 into an array?Take a look at the range function.

If I have a variable $num = 50 how can I put the numbers 1-开发者_Go百科50 into an array?


Take a look at the range function.

$array = range(1, $num);


Have a look at the documentation for the range() function:

<?php

    $array = range(1, 50);

?>


This can be solved by using a simple for loop:

//  Start ↓    End ↓  Step ↓
for ($i = 1; $i <= $num; ++$i) {
    $array[] = $i;
}


I think the reason why range() wasn't accepted is that the array needed to start with 1; So:

$array=range(0,$num);
unset($array[0]);
0

精彩评论

暂无评论...
验证码 换一张
取 消