Alireza Peer
Alireza Peer

Reputation: 908

Add data to JSON child array

This is my JSON file(database.json):

{
  "doctors": [
    {
      "ID": "ahmadakhavan",
      "pass": "1234",
      "name": "Ahmad Akhavan",
      "profilePic": "address",
    },
    {
      "ID": "akramparand",
      "pass": "1234",
      "name": "Akram Parand",
      "profilePic": "address",
    }
  ],
  "games": [
    {
      "ID": "shuttlefuel_1",
      "locked": "0",
      "logo": "gameLogo",
    },
    {
      "ID": "birthdaycake",
      "locked": "0",
      "logo": "gameLogo",
    }
  ],
  "users": [
    {
      "ID": "alirezapir",
      "prescribes": [
        {
          "doctorName": "doctor1",
          "done": "yes",
          "gameId": "wordschain"
        },
        {
          "doctorName": "doctor2",
          "done": "no",
          "gameId": "numberlab"
        }
      ],
      "profilePic": "address"
    },
    {
      "ID": "amirdibaei",
      "pass": "1234",
      "profilePic": "address"
    }
  ]
}

I want to add a child under prescribes array for a specific ID.

Below is what I have done in my PHP code to do this:

 <?php 
  $username = $_REQUEST['name'];
  $data = $_REQUEST['data'];

  //Load the file
 $contents = file_get_contents('./database.json');
  $arraydata = json_decode($data,true);
 //Decode the JSON data into a PHP array.
 $contentsDecoded = json_decode($contents, true );
     foreach($contentsDecoded['users'] as $item){
         if($item['ID'] == $username){
             if(!isset($item['prescribes'])){
                 $item['prescribes'] = Array();
             }
             array_push($item['prescribes'],$arraydata);
            $json = json_encode($contentsDecoded, JSON_UNESCAPED_UNICODE );
            file_put_contents('./database.json', $json);  
             exit('1');
             exit;
         }
     }
    exit('0'); 
    exit;
 ?> 

If I echo $item['prescribes'] after the line array_push($item['prescribes'],$arraydata); I see data added to it, but the original file (database.json) won't show new added data.

(meaning that this new data is not added to $contentsDecoded)

Upvotes: 2

Views: 78

Answers (2)

madalinivascu
madalinivascu

Reputation: 32354

Change your foreach to change the $contentsDecoded array:

foreach($contentsDecoded['users'] as $key => $item){
         if($item['ID'] == $username){
             if(!isset($item['prescribes'])){
                 $contentsDecoded['users'][$key]['prescribes'] = Array();
             }
             array_push($contentsDecoded['users'][$key]['prescribes'],$arraydata);
            $json = json_encode($contentsDecoded, JSON_UNESCAPED_UNICODE );
            file_put_contents('./database.json', $json);  
             exit('1');
             exit;
         }
     }

Upvotes: 1

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You have to change foreach() code like below:-

foreach($contentsDecoded['users'] as &$item){ //& used as call by reference
    if($item['ID'] == $username){
        $item['prescribes'][] = $arraydata; //assign new value directly
        $json = json_encode($contentsDecoded, JSON_UNESCAPED_UNICODE );
        file_put_contents('./database.json', $json);  
        exit;
    }
}

Upvotes: 1

Related Questions