2

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)

4
  • what a "database":)) its a file man a json file :))) Commented May 21, 2019 at 10:44
  • @madalinivascu Ok :)) its just a tiny JSON file... Commented May 21, 2019 at 10:46
  • why aren't you doing if(!isset($item['prescribes'])){ $item['prescribes'] =[$arraydata]; } ? Commented May 21, 2019 at 10:48
  • @madalinivascu because i'm ADDING new item to prescribes array, i don't want to completely replace it :) Commented May 21, 2019 at 10:49

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.