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

2022-01-31 00:00:00 arrays 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.

相关文章