Merge two arrays as key value pairs in PHP(在 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 中将两个数组合并为键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中将两个数组合并为键值对


基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01