猿问

将数据添加到 JSON 子数组

这是我的 JSON 文件(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"

    }

  ]

}

我想在prescribes数组下为特定 ID添加一个子项。


下面是我在 PHP 代码中所做的:


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

 ?> 

如果我$item['prescribes']在该行之后回显,array_push($item['prescribes'],$arraydata);我会看到添加了数据,但原始文件 ( database.json) 不会显示新添加的数据。


qq_遁去的一_1
浏览 139回答 2
2回答

白衣非少年

您必须更改foreach()如下代码:-foreach($contentsDecoded['users'] as &$item){ //& used as call by reference&nbsp; &nbsp; if($item['ID'] == $username){&nbsp; &nbsp; &nbsp; &nbsp; $item['prescribes'][] = $arraydata; //assign new value directly&nbsp; &nbsp; &nbsp; &nbsp; $json = json_encode($contentsDecoded, JSON_UNESCAPED_UNICODE );&nbsp; &nbsp; &nbsp; &nbsp; file_put_contents('./database.json', $json);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; }}

qq_花开花谢_0

更改您的 foreach 以更改 $contentsDecoded 数组:foreach($contentsDecoded['users'] as $key => $item){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if($item['ID'] == $username){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(!isset($item['prescribes'])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$contentsDecoded['users'][$key]['prescribes'] = Array();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array_push($contentsDecoded['users'][$key]['prescribes'],$arraydata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $json = json_encode($contentsDecoded, JSON_UNESCAPED_UNICODE );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_put_contents('./database.json', $json);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit('1');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;}
随时随地看视频慕课网APP
我要回答