2016-11-17 1 views
1

Ich habe einfach bestehende JSON wie diesefusionieren neue json Element als Array in JSON

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

Wenn ich jetzt Schaltfläche Speichern klicken Sie auf Ich habe einige Daten wie „ConsultantInfo“ und „Otherinfo“ zu bestehenden JSON-Daten anhängen Ich habe dies unter Verwendung von Code getan.

Nach der Verwendung dieses Codes wird mein Json wie folgt aussehen, bis dies alles in Ordnung ist.

[{ 
     "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" 
     } 
}] 

Problem ist, ich mehrmals speichern Schaltfläche klicken kann ConsultantInfo und Otherinfo hinzufügen, sollte es wie ein Array in JSON-Datei sein. wie geht das?.

Ausgabe sollte LIKE

[{ 
      "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" 
       } 
      ] 
    }] 
+0

fügen Sie wird dazu beitragen, was Sie bisher –

Antwort

1

Hier ist der Code, der ConsultantInfo und Otherinfo als sametime compate, nur ther beide sind nicht in der $ state_current_arr, werden sie in hinzugefügt werden.

Demos

exit neue Daten vorhanden sind, so dass die neuen Daten hinzugefügt nicht in

not exist neue Daten hinzugefügt, wenn nicht vorhanden.

hoffe es hilft. hier ist der Code,

<?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; 
    ?> 
2

Überprüfen Sie, ob, dass Sie Ihr Ergebnis

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

    ?> 
+0

die Frage versucht haben wollen ConsultantInfo und Otherinfo zugleich vergleichen , nicht getrennt. –