PHP: how to fill an array with keys and values so 1 = 1, 2 = 2, 3 = 3 etc
I am looking to do something like (psuedo_code)
$myarray = fill_array_keys_and_values_from_parameter1_until_parameter2(18, 50);
So that I get
$myarray= array(
'18' => '18',
'1开发者_如何学运维9' => '19',
...
'50' => '50'
)
without having to a for loop ideally. Is there such a PHP function, I had a browse of the manual but could not see what I was looking for.
Thanks in advance
I don't think there is a specific function that can do this (although there are a couple that come close.)
What about doing this?
$values = range(18, 50);
$array = array_combine($values, $values);
Using a for loop:
$arr = array();
foreach (range(18, 50) as $i) {
$arr[$i] = $i;
}
simshaun's solution is much better, though.
精彩评论