PHP Array - Turning Array values into Keys

2020-03-01 08:09发布

What is the MOST EFFICIENT way to have an array of values and turn it into an array of keys? I'd really like to avoid any foreach loop...

$in = array(
    'red',
    'green',
    'blue'
);

INTO

$out = array(
    'red' => NULL,
    'green' => NULL,
    'blue' => NULL
);

标签: php arrays key
1条回答
小情绪 Triste *
2楼-- · 2020-03-01 08:47

Use PHP's array_flip function.


On second thought, if you want the values to be null, then you might want to use array_fill_keys:

$out = array_fill_keys($in, null);
查看更多
登录 后发表回答