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',
'19' => '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
Using the fill() method The fill() method, fills the elements of an array with a static value from the specified start position to the specified end position. If no start or end positions are specified, the whole array is filled. One thing to keep in mind is that this method modifies the original/given array.
The array_keys() function returns an array containing the keys.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With