How to dynamically create an array from 10 to 100 with a space of 10 between the values?

Can someone tell me how I can create an array of numbers dynamically, without any loops?

eg. I want to create an array like:

[0] => 10 [1] => 20 [2] => 30 [3] => 40 .. [9] => 100 
+7
arrays php
source share
5 answers

You can use range() . The third argument is the number to jump between the values ​​when interpolating between the start and end values.

 $numbers = range(10, 100, 10); 
+27
source share

Good answers are in front of me, but best of all, that matches your purpose, range(start, end, step) as follows:

 $numbers = range(10, 100, 10); var_dump($numbers); 
+7
source share

using

 $numbers = range(10, 100, 10); 

It will create arrays starting from 10 to 100 with 10 steps.

+5
source share

1. You can use the for loop as shown below (do not hate the for loop): -

 <?php $numbers = array(); for($i=10;$i<=100;$i=$i+10) { $numbers[] = $i; } print_r($numbers); ?> 

Exit: - https://eval.in/612601

2. range() (better): -

 <?php $numbers = range(10, 100, 10); print_r($numbers); ?> 

Exit: - https://eval.in/612607

+4
source share

You can use several methods.

Use the for loop:

 $dynamic_array = []; for($i = 10; $i <= 100; $i += 10){ $dynamic_array[] = $i; } print_r($dynamic_array); 

Use the do, while loop:

 $dynamic_array2 = []; $i = 10; do{ $dynamic_array2[] = $i; $i += 10; } while($i <= 100); print_r($dynamic_array2); 

Use the Range () function:

 $dynamic_array3 = range(10, 100, 10); print_r($dynamic_array3); 

I would suggest a range function because it is the shortest and easiest.

+4
source share

All Articles