Reputation: 45
I am trying to parse JSON coming in from a Firebase Realtime Database instance, and display it in a table.
I can return the JSON from the URL and store it in PHP but I can't seem to get it to display.
The JSON Output looks like this:
{"\"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"}}}
In PHP I am doing the following to grab the data:
<?php
$json_string_itemData = file_get_contents("ENDPOINT/items.json");
$itemData = json_decode($json_string_itemData);
?>
And in the table I am doing the following:
<?php
foreach ($itemData as $item)
{
echo '<td>' . $item->itemName . '</td>';
}
?>
I get the error Warning: Invalid argument supplied for foreach() in test.php. The line error is the one mentioned above.
Any idea how I can parse the data?
Upvotes: 0
Views: 493
Reputation: 188
add true to make it array
$itemData = json_decode($json_string_itemData, true);
try this for loop
foreach ($itemData as $item)
{
echo '<td>' . $item['itemDescription'] . '</td>';
}
Upvotes: 1