encode json using php?

前端 未结 5 1759
滥情空心
滥情空心 2020-12-06 07:33

I want to get json with php encode function like the following



        
5条回答
  •  鱼传尺愫
    2020-12-06 08:19

    Do the json_encoding as the LAST step. Build your data structure purely in PHP, then encode that structure at the end. Doing intermediate encodings means you're basically building your own json string, which is always going to be tricky and most likely "broken".

    $data = array();
    while ($row = mysql_fetch_array($result)) {
       $data[] = array('id'=>$i, 'name' => $row['name']);
    }
    echo json_encode($data);
    

提交回复
热议问题