這是一個(gè)通用參考問題和答案,涵蓋許多永無休止的“如何訪問 JSON 中的數(shù)據(jù)?”問題。它在這里處理在 PHP 中解碼 JSON 并訪問結(jié)果的廣泛基礎(chǔ)知識(shí)。
我有 JSON:
{ "type": "donut", "name": "Cake", "toppings": [ { "id": "5002", "type": "Glazed" }, { "id": "5006", "type": "Chocolate with Sprinkles" }, { "id": "5004", "type": "Maple" } ] }
如何在 PHP 中對(duì)其進(jìn)行解碼并訪問結(jié)果數(shù)據(jù)?
在此示例中,json_decode() 用于將 JSON 數(shù)據(jù)解碼為 PHP 關(guān)聯(lián)數(shù)組。然后,您可以像訪問任何 PHP 數(shù)組一樣訪問該數(shù)組的各個(gè)元素。