Merging two json in PHP(在 PHP 中合并两个 json)
问题描述
我有两个 json
第一个是
[{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number"}
,{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number"}]
第二个是
[{"COLUMN_NAME":"ORDER_NO","DEFAULT_VALUE":"1521"},
{"COLUMN_NAME":"CUSTOMER_NO","DEFAULT_VALUEE":"C1435"}]
我想合并它们并有一个像
I want to merge them and have a json like
[{"COLUMN_NAME":"ORDER_NO","COLUMN_TITLE":"Order Number","DEFAULT_VALUE":"1521"}
,{"COLUMN_NAME":"CUSTOMER_NO","COLUMN_TITLE":"Customer Number","DEFAULT_VALUEE":"C1435"}]
有没有办法合并它们?如果需要更改 JSON 中的结构,对我来说也可以
is there a way to merge them? It is also OK for me if a stucture change in JSON is required
谢谢.
推荐答案
类似 json_encode(array_merge(json_decode($a, true),json_decode($b, true)))
之类的东西应该可以工作.
Something like json_encode(array_merge(json_decode($a, true),json_decode($b, true)))
should work.
array_merge PHP 官方文档
array_merge in official PHP documentation
json_decode 在 PHP 官方文档中
json_decode in official PHP documentation
尝试将 true
作为第二个参数添加到 json_decode.这会将对象转换为关联数组.
try adding true
as second parameter to json_decode. That'll convert objects to associative arrays.
EDIT 2:尝试 array-merge-recursive
并在下面查看我的评论.抱歉现在必须退出:(这看起来是一个完整正确的解决方案:https://stackoverflow.com/a/20286594/1466341
EDIT 2: try array-merge-recursive
and see my comment below. Sorry have to log out now :(
This looks like a full correct solution: https://stackoverflow.com/a/20286594/1466341
这篇关于在 PHP 中合并两个 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PHP 中合并两个 json


基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01