简体   繁体   English

我如何在PHP中合并两个以上的Json字符串

[英]How i can merge more than two Json strings in php

Below is my JSON strings like these, am trying to add jsons. 以下是我的此类JSON字符串,正在尝试添加json。 How i can achieve. 我怎么能实现。

$json1 = {"properties":{"title":"test","labels":["JanActual","Jan","Goal"],"values":["0","10000","0"]}}
$json2 = {"key":"Rental","type":"bar","values":["0","10000","0"]}
$json3 = {"key":"Service","type":"bar","values":["189","30000","0"]}

I am trying to use this to merge them and am expecting output like below 我正在尝试使用它来合并它们,并期望输出如下

   {
    "properties":{
        "title":"test",
        "labels":[
            "JanActual",
            "Jan",
            "Goal"
        ],
        "values":[
            "0",
            "10000",
            "0"
        ]
    },
    "data": [
        {
            "key":"Rental",
            "type":"bar",
            "values":[
            "0",
            "10000",
            "0"
            ]
        },
        {
            "key":"Service",
            "type":"bar",
            "values":[
                "189",
                "30000",
                "0"
            ]
        }
    ]
}

Any help? 有什么帮助吗?

Decode json to php arrays, merge and encode back 将json解码为php数组,合并并重新编码

$json1 = json_decode($json1, true);
$json1['data'] = array(
  json_decode($json2, true),
  json_decode($json3, true)
);

echo json_encode($json1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM