本文目錄一覽:
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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/259605.html