I am using the range()
function to create an array. However, I want the keys
to be the same as the value
. This is ok when i do range(0, 10)
as the index starts from 0
, however if i do range(1, 11)
, the index will still start from 0
, so it ends up 0=>1
when i want it to be 1=>1
How can I use range()
to create an array where the key
is the same as the value
?
keyN => valueN, ]; Note: The comma after the last Key-Value pair is optional. The Key can be of either integer or string type.
Arrays contains unique key. Hence if u are having multiple value for a single key, use a nested / multi-dimensional array. =) thats the best you got.
Associative arrays - Arrays with named keys.
How about array_combine?
$b = array_combine(range(1,10), range(1,10));
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