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

查看:379
本文介绍了在 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.

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

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