duanchan9354 2015-06-03 14:43
浏览 51
已采纳

使用PHP从json数据中获取内容

I have a question. How to get content only from json data.

My JSON Data:

array(
(int) 0 => '{"html":"Lorem Ipsum \u2013 Lorem Ipsum \u2013 Lorem Ipsum - Lorem Ipsum \u2013 Lorem Ipsum \u2013 Lorem Ipsum \u2013 Lorem Ipsum"}',
(int) 1 => '{"text":"Lorem Ipsum"}',
(int) 2 => '{"text":"01 June-30 September"}',
(int) 3 => '{"text":"7 nights \/ 8 days"}',
(int) 4 => '{"text":"Lorem Ipsum"}'
);


I want to get html and text content only

Lorem Ipsum
Lorem Ipsum
01 June-30 September

I tried to use json_decode.

My Code:

foreach($array as $i) {
        $a = json_decode($i);
        echo $a;
}

Anyone help me please?

  • 写回答

1条回答 默认 最新

  • doufu1504 2015-06-03 15:07
    关注

    Assume your data is stored in a variable called $a.

    echo json_decode($a[0], true)['html'];

    will get you the result for the first index of the array. For the other indexes you would use text instead of html.

    Note I've passed true for the second argument of json_decode, if you prefer to treat the decoded data as an object you can leave that argument off, in which case the code would become

    echo json_decode($a[0])->html;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题