DadaB DadaB - 1 year ago 70
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 Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download