Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Create an array for a range

If I have a variable $num = 50 how can I put the numbers 1-50 into an array?

like image 646
tarnfeld Avatar asked Nov 21 '09 12:11

tarnfeld


People also ask

What is Array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

What is range array?

The range of an array is the difference between the maximum element of the array and the minimum element. You want to find the minimum range of the array by doing any number of operations on the array. You can do multiple operations on a single element as well.


2 Answers

Take a look at the range function.

$array = range(1, $num);
like image 192
erenon Avatar answered Sep 18 '22 06:09

erenon


This can be solved by using a simple for loop:

//  Start ↓    End ↓  Step ↓
for ($i = 1; $i <= $num; ++$i) {
    $array[] = $i;
}
like image 21
tarnfeld Avatar answered Sep 21 '22 06:09

tarnfeld