首页 > 编程知识 正文

php遍历输出json(php遍历输出数组)

时间:2023-12-23 21:12:50 阅读:319513 作者:ESXX

本文目录一览:

PHP如何循环json?

需要先将json转换成数组,然后才能循环。

json是字符串,不能直接循环。使用 json_decode($jsonstring, true) 可以将格式正确的json字符串转换成关联数组。

需要注意,该函数只能处理UTF-8编码的json字符。

实例代码:

?php

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));

var_dump(json_decode($json, true));

?  

以上实例将会输出:

object(stdClass)#1 (5) {

    ["a"] = int(1)

    ["b"] = int(2)

    ["c"] = int(3)

    ["d"] = int(4)

    ["e"] = int(5)

}

array(5) {

    ["a"] = int(1)

    ["b"] = int(2)

    ["c"] = int(3)

    ["d"] = int(4)

    ["e"] = int(5)

}

?

php 循环遍历json数据

$str = '{

"10924": {

"id": "10924",

"title": "天津",

"streamline_title": "狗不理",

"unit": "点",

"goods_type": "168",

"goods_type_title": "包子"

},

"10923": {

"id": "10923",

"title": "北京",

"streamline_title": "王府井",

"unit": "点",

"goods_type": "104",

"goods_type_title": "吃货天堂"

},

"11982": {

"id": "11982",

"title": "南京",

"streamline_title": "夫子庙",

"unit": "点",

"goods_type": "351",

"goods_type_title": "灯会"

}

}';

foreach (json_decode($str) as $v)

{

echo "{$v-id} {$v-title}"; //其他的一样的

}

php遍历目录下的全部文件 生成图片显示那样的json 不知道怎么实现了

$item = array();

$item["name"] = "123456";

$items = array();

$items[] = $item;

// 上面的循环生成添加多个

$dirs = array();

$dirs["abc"] = $items;

// 输出即可

echo json_encode($dirs);

PHP怎么解析这段json代码,并且要循环输出来

$str=json_decode('你的json');

var_dump($str-info-item[0]);//取出第一个item

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。