优雅的方式来合并两个数组在PHP中键值对? [英] Elegant way to merge two arrays as key value pairs in PHP?

查看:555
本文介绍了优雅的方式来合并两个数组在PHP中键值对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了相同大小的两个数组。我想合并这两个这么一的值是新数组的关键指标,而新的数组的值是其他的值。

现在我只是通过数组循环和手动创建新的数组,但我有一种感觉,有一种更优雅的方式去了解这一点。我没有看到任何数组功能用于此目的,但也许我错过了什么?有没有一种简单的方法来此沿着这些路线?

  $ mapped_array =映射键($ array_with_keys,$ array_with_values​​);


解决方案

请参见 array_combine() 的上PHP.net。

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);

解决方案

See array_combine() on PHP.net.

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

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