在 PHP 中合并两个数组作为键值对

问题描述:

我有两个相同大小的数组.我想合并这两个,所以一个的值是新数组的关键索引,新数组的值是另一个的值.

I've got two arrays of the same size. I'd like to merge the two so the values of one are the key indexes of the new array, and the values of the new array are the values of the other.

现在我只是循环遍历数组并手动创建新数组,但我觉得有一种更优雅的方法来解决这个问题.我没有看到任何用于此目的的数组函数,但也许我错过了什么?有没有一种简单的方法可以做到这一点?

Right now I'm just looping through the arrays and creating the new array manually, but I have a feeling there is a much more elegant way to go about this. I don't see any array functions for this purpose, but maybe I missed something? Is there a simple way to this along these lines?

$mapped_array = mapkeys($array_with_keys, $array_with_values);

参见 array_combine() 在 PHP.net 上.

See array_combine() on PHP.net.