Reputation:
Easy question, I filled my array the following way:
$options = range(1, 10);
This will result:
array
(
[0] => 1
[1] => 2
etc. etc.
)
This is not the result I want..
I need my array like this:
array
(
[1] => 1
[2] => 2
etc.
)
How to accomplish this easy task?
Upvotes: 2
Views: 170
Reputation: 516
function myRange($start, $limit, $step)
{
$myArr = array();
foreach((array) range($start, $limit,$step) as $k => $v)
{
$myArr[$k+1] = $v;
}
return $myArr;
}
print_r(myRange(0, 100, 10));
?>
Result ------
Array
(
[1] => 0
[2] => 10
[3] => 20
[4] => 30
[5] => 40
[6] => 50
[7] => 60
[8] => 70
[9] => 80
[10] => 90
[11] => 100
)
Upvotes: 2
Reputation: 835
Or just shift the array
foreach ( $array as $key => $val )
$result[ $key+1 ] = $val;
Upvotes: 0
Reputation: 51817
if you want a one-liner instead of a for-loop like Berry suggested, just use array_combine:
$array = array_combine(range(1,10),range(1,10));
Upvotes: 2
Reputation: 18859
<?php
for( $i = 1; $i <= 10; $i ++ ) {
$array[$i] = $i;
}
Voila. :)
Upvotes: 3