Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically create an array of 10 to 100 with gap of 10 between values? [duplicate]

Tags:

arrays

php

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

e.g. I want to create array like:

 [0] => 10
 [1] => 20
 [2] => 30
 [3] => 40
    ..
 [9] => 100
like image 326
Dhara Parmar Avatar asked Jul 27 '16 07:07

Dhara Parmar


People also ask

How do you declare an array dynamically?

Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator. Use the delete operator with [] to free the memory of all array elements.

Can we create a dynamic array?

A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required.

What is dynamic array with example?

A dynamic array is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard libraries in many modern programming languages. Dynamic arrays overcome a limit of static arrays, which have a fixed capacity that needs to be specified at allocation.

Can lists grow & shrink dynamically?

Java arrays are of static size. You should use a List (whose most frequently used implementation is ArrayList ) instead, which can grow and shrink dynamically and safely.


Video Answer


2 Answers

You could use range(). The third argument is the number to step between values when interpolating between the start and ending values.

$numbers = range(10, 100, 10); 
like image 103
alex Avatar answered Sep 19 '22 13:09

alex


Good answers before me, but the best, what match exactly your assignment is use range(start, end, step) this way:

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

var_dump($numbers);
like image 45
von Oak Avatar answered Sep 21 '22 13:09

von Oak