err*_*ata 5 php arrays dictionary associative-array
我有一个简单的数组,其中key 总是后面跟着值:
Array (
[0] => x
[1] => foo
[2] => y
[3] => bar
)
Run Code Online (Sandbox Code Playgroud)
我想将其转换为关联的:
Array (
[x] => foo
[y] => bar
)
Run Code Online (Sandbox Code Playgroud)
最简单,最优雅的方法是什么?
提高内存效率,减少计算量。
如果$ input数组具有奇数个值,则最后一个值将为NULL。
$result = array();
while (count($input)) {
$result[array_shift($input)] = array_shift($input);
}
Run Code Online (Sandbox Code Playgroud)