将两个数组合并为PHP中的键值对 [英] Merge two arrays as key value pairs in PHP

查看:89
本文介绍了将两个数组合并为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);

推荐答案

请参阅PHP.net上的 array_combine()

See array_combine() on PHP.net.

这篇关于将两个数组合并为PHP中的键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆