我正在嘗試解析來自 Firebase 實時數據庫實例的 JSON,并將其顯示在表格中。我可以從 URL 返回 JSON 并將其存儲在 PHP 中,但我似乎無法顯示它。JSON 輸出如下所示:{"\"1\"":{"itemDescription":"This is a description of the item.","itemName":"Item Name","itemValue":{"costAmount":200,"costType":"dol"}},"\"2\"":{"itemDescription":"This is an item description.","itemName":"Item Name 2","itemValue":{"costAmount":500,"costType":"dol"}}}在 PHP 中,我正在執行以下操作來獲取數據:<?php$json_string_itemData = file_get_contents("ENDPOINT/items.json");$itemData = json_decode($json_string_itemData);?>在表中,我正在執行以下操作:<?phpforeach ($itemData as $item){ echo '<td>' . $item->itemName . '</td>';}?>我在 test.php 中收到錯誤警告:為 foreach() 提供的參數無效。線路錯誤就是上面提到的那個。知道如何解析數據嗎?
1 回答

PIPIONE
TA貢獻1829條經驗 獲得超9個贊
添加 true 使其成為數組
$itemData = json_decode($json_string_itemData, true);
試試這個 for 循環
foreach ($itemData as $item)
{
echo '<td>' . $item['itemDescription'] . '</td>';
}
- 1 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消