Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

php how to make value 5 become array 1,2,3,4,5

It’s possible to make array from single number in PHP?
I have value $val = 5; and I want to convert to array as $val = [1,2,3,4,5].
I try like this but return empty array.

$val = 5;
for ($i = 0; $i < $val; $i++;) {
   $newArray = [
    'no' => $val[$i],
   ];
}

There is any function in PHP to achieve it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, you can achieve it by using range():

$val = 5;
$newArray = range(1, $val); # start = 1, end = $val

More info: https://www.php.net/manual/en/function.range.php

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading