Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP create array where key and value is same

Tags:

arrays

php

range

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?

like image 479
Ozzy Avatar asked Mar 19 '11 05:03

Ozzy


People also ask

How do I create an array value as an array key in PHP?

keyN => valueN, ]; Note: The comma after the last Key-Value pair is optional. The Key can be of either integer or string type.

Can an array have same key PHP?

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.

Which array has pair of value and key in PHP?

Associative arrays - Arrays with named keys.


1 Answers

How about array_combine?

$b = array_combine(range(1,10), range(1,10)); 
like image 145
Mason Avatar answered Sep 19 '22 17:09

Mason