DadaB DadaB - 25 days ago 7
PHP Question

how to convert integer to array of elements equal to that integer

So I have string which can be converter to integer if needed:

$pages = '154';


How do I convert it into array like this:

array (
[0] => 1,
[1] => 2,
[2] => 3,
[3] => 4,
....
[153] => 154);

Answer

Just create a range:

$result = range(1, $pages);

For more flexibility if you have other numbers to start or end on; create a range for the keys and a range for the values and combine:

$result = array_combine(range(0, $pages-1), range(1, $pages));