doukengsha9472 2013-06-06 21:54
浏览 48
已采纳

向JSON添加新行

I have successfully get content from the database and output the results in JSON. But I want to add a text that doesn't exists in the database and it's here I'm stuck.

$statement = $sql->prepare("SELECT data_filename,
                                   data_filetype,
                                   data_uniqid,
                                   data_description,
                                   data_coordinates,
                                   exif_taken,
                                   exif_camera,
                                   exif_camera_seo,
                                   exif_resolution,
                                   exif_sensitivity,
                                   exif_exposure,
                                   exif_aperture,
                                   exif_focallength,
                                   is_downloadable,
                                   is_notaccurate,
                                   allow_fullsize

                            FROM photos
                            WHERE data_filename = 'P1170976'");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);

$json = json_encode($results);

echo $json;

That code gives me

[{"data_filename":"P1170976","data_filetype":"JPG","data_uniqid":"","data_description":"","data_coordinates":"","exif_taken":"0000-00-00","exif_camera":"","exif_camera_seo":"","exif_resolution":"","exif_sensitivity":"0","exif_exposure":"","exif_aperture":"","exif_focallength":"","is_downloadable":null,"is_notaccurate":null,"allow_fullsize":null}]

Which is correct of course but if I add these 2 new lines under $json = json_encode... I'm getting null.

$newdata = array('test' => 'just testing');
$json[] = $newdata;

What have I done wrong here?

  • 写回答

2条回答 默认 最新

  • dousha7904 2013-06-06 21:58
    关注

    json_encode() returns a string, so you can’t handle it as an array, i.e. add elements to string.

    As noted in comments, you need to add those lines before json_encode() or decode it back to array using json_decode(), then apply the lines and then back json_encode().

    Example about usage of json_encode and json_decode:

    $array = array("this" => array("will" => array("be" => "json")));
    $string = json_encode($array); // string(31) "{"this":{"will":{"be":"json"}}}"
    
    // ...
    
    $array2 = json_decode($string); // now it’s same array as in first $array
    $array2["new"] = "element";
    $string2 = json_encode($array2);
    var_dump($string2); // string(46) "{"this":{"will":{"be":"json"}},"new":"string"}"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行