Convert list (array) to associative array in PHP

I want to convert a PHP list (array), i.e.

array("start", "end", "coords")

      

into an associative array with the right values ​​(just to quickly check for the presence / absence of a key), that is, something like this:

array(
    "start" => 1,
    "end" => 1,
    "coords" => 1
)

      

Is there a more elegant way to do this than this?

array_fill_keys($ar, 1)

      

+3


source to share


2 answers


There is probably no more elegant solution than array_fill_keys($ar, 1)

.



+5


source


This feature is called array_flip

.

http://php.net/array_flip



Executing array_flip

in an array and then using isset

it turned out to be much faster than it was in_array

for me.

But note that this is only useful if you intend to search the array multiple times.

+1


source







All Articles