将新的json元素合并为JSON中的数组

时间:2016-11-17 07:00:32

标签: php json

我有像这样的简单现有JSON

 [{
    "id": "77",
    "agent_id": "20",
    "raised_by": "C",
    "from_date": "2016-11-09",
    "to_date": "2016-11-10"
}]

现在当我点击保存按钮时,我必须将一些数据如“ConsultantInfo”和“otherInfo”附加到现有的JSON数据中,我使用下面的代码执行此操作。

<?php

    $state_current_arr = '{ "id": "77", "agent_id": "20", "raised_by": "C", "from_date": "2016-11-09", "to_date": "2016-11-10" }';
    if(isset($_POST['savebtn'])) //save button
    {
    $jsonData = array();
    $jsonData['ConsultantInfo'] = array("user_id"=>"3045","inquiry_id"=>"77");
    $jsonData['otherInfo'] = array("a"=>"test","b"=>"testing");

    $tempArray = json_decode($state_current_arr,true);
    $tempArray = array_merge($tempArray, $jsonData);
    $jsonData_merged = json_encode($tempArray);

    echo $jsonData_merged;
    }
    ?>

使用此代码后,我的json将如下所示,直到这一切都好。

[{
        "id": "77",
        "agent_id": "20",
        "raised_by": "C",
        "from_date": "2016-11-09",
        "to_date": "2016-11-10",
        "ConsultantInfo": {
            "user_id": "3045",
            "inquiry_id": "77"
        },
        "otherInfo": {
            "a": "test",
            "b": "testing"
        }
}]

问题是,我可以多次点击保存按钮添加ConsultantInfo和otherInfo,它应该像json文件中的数组一样。该怎么做?。

输出应该像

[{
            "id": "77",
            "agent_id": "20",
            "raised_by": "C",
            "from_date": "2016-11-09",
            "to_date": "2016-11-10",
            "ConsultantInfo": [
                {
                "user_id": "3045",
                "inquiry_id": "77"
                },
                {
                "user_id": "2",
                "inquiry_id": "71"
                } 
            ],
            "otherInfo": [
              {
                "a": "test",
                "b": "testing"
              },
              {
                "a": "kk",
                "b": "dd"
              } 
            ]
    }]

2 个答案:

答案 0 :(得分:2)

检查这是否有助于您获得结果

 <?php



        $state_current_arr = '{ "id": "77", "agent_id": "20", "raised_by": "C", "from_date": "2016-11-09", "to_date": "2016-11-10" }';
        $newInfo ='{

            "a": "kk",
             "b": "dd"

        }';

        $newConsult ='{
            "user_id": "2",
            "inquiry_id": "71"
        }';

        $jsonData = array();
        $jsonData['ConsultantInfo'] =["{ 'user_id':'3045','inquiry_id':'77'}"];//array("user_id"=>"3045","inquiry_id"=>"77");
        $jsonData['otherInfo'] =["{ 'a':'test','b':'testing'}"];// array("a"=>"test","b"=>"testing");
            if(isset($newConsult))
            {
                $newConsult =json_decode($newConsult,true);
                array_push($jsonData['ConsultantInfo'],$newConsult);
                $tempArray = json_decode($state_current_arr,true);
                $tempArray = array_merge($tempArray, $jsonData);

            }
            if(isset($newInfo))
            {  
                $newInfo =json_decode($newInfo,true); 
                array_push($jsonData['otherInfo'],$newInfo);
                $tempArray = json_decode($state_current_arr,true);
                $tempArray = array_merge($tempArray, $jsonData);
            }






        $jsonData_merged = json_encode($tempArray);

        echo $jsonData_merged;

     ?>

答案 1 :(得分:1)

以下是将ConsultantInfo和otherInfo组合为同一时间的代码,只有两者都不在$ state_current_arr中,它们将被添加到。

<强>演示

exit新数据已存在,因此未添加新数据

not exist新数据在不存在时添加。

希望它有所帮助。 这是代码,

   <?php
    $state_current_arr = '{"id":"77","agent_id":"20","raised_by":"C","from_date":"2016-11-09","to_date":"2016-11-10","ConsultantInfo":[{"user_id":"3045","inquiry_id":"77"}],"otherInfo":[{"a":"test","b":"testing"}]}';
//    $state_current_arr = '{ "id": "77", "agent_id": "20", "raised_by": "C", "from_date": "2016-11-09", "to_date": "2016-11-10" }';
    $jsonData = array();
    $jsonData['ConsultantInfo'] = array("user_id"=>"3045","inquiry_id"=>"77");
    $jsonData['otherInfo'] = array("a"=>"test","b"=>"testing");

    $tempArray = json_decode($state_current_arr,true);


    if(empty($tempArray['ConsultantInfo']) || empty($tempArray['otherInfo']) || count(
        array_filter($tempArray['ConsultantInfo'], function($v, $k)use($jsonData, $tempArray){
            return ($v["user_id"] == $jsonData['ConsultantInfo']["user_id"]) && ($v["inquiry_id"] == $jsonData['ConsultantInfo']["inquiry_id"]) && ($tempArray[$k]['otherInfo']["a"] == $jsonData["inquiry_id"]["a"]) && ($tempArray[$k]['otherInfo']["b"] == $jsonData["inquiry_id"]["b"]) ? true : false;
        }, ARRAY_FILTER_USE_BOTH
    )) == 0){
        $tempArray['ConsultantInfo'][] = $jsonData['ConsultantInfo'];
        $tempArray['otherInfo'][] = $jsonData['otherInfo'];
    }

    $jsonData_merged = json_encode($tempArray);

    echo $jsonData_merged;
    ?>