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;

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

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)