PHP数组 - 将数组值转换为键

Ale*_*x V 13 php arrays key

获得一系列值并将其转换为键数组的最有效方法是什么?我真的想避免任何foreach循环......

$in = array(
    'red',
    'green',
    'blue'
);
Run Code Online (Sandbox Code Playgroud)

INTO

$out = array(
    'red' => NULL,
    'green' => NULL,
    'blue' => NULL
);
Run Code Online (Sandbox Code Playgroud)

Tra*_*ty3 31

使用PHP的array_flip功能.


第二个想法,如果你想要值为null,那么你可能想要使用array_fill_keys:

$out = array_fill_keys($in, null);
Run Code Online (Sandbox Code Playgroud)