PHP create array where key and value is same

Ozzy picture Ozzy · Mar 19, 2011 · Viewed 23.9k times · Source

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?

Answer

Mason picture Mason · Mar 19, 2011

How about array_combine?

$b = array_combine(range(1,10), range(1,10));