Reputation: 215
i have a json data like so
{
"code": 1,
"data": [
{
"apple": [
{
"id": 127,
"type": 1,
"color": green,
"stage": 1,
"status": 1
},
{
"id": 128,
"type": 2,
"color": red,
"stage": 1,
"status": 1
}
]
},
{
"oranges": [
{
"id":133
"type": 3,
"color": rainbow,
"stage": 1,
"status": 1
},
{
"id":134
"type": 3,
"color": black,
"stage": 1,
"status": 1
}
]
},
{
"berry": [
{
"id":4
"type": 2,
"color": white,
"stage": 1,
"status": 1
}
]
},
{
"watermelon": [
{
"id":5
"type": 2,
"color": red and blue,
"stage": 1,
"status": 1
}
]
}
],
"bleh": "Succesfully queried database"
}
i would like to create a table in php that goes somthing like this
Fruit | Type | color
apple 1 green
apple 2 red
oranges 3 rainbow
oranges 3 black
so basically what i want is when ever a object like apple has more then one array inside it the table to display apple
and the corresponding data to it.
this is what i have so far
$output = json_decode(JsonData);
$result =$output['data'][0]['apple'];
<table>
<thead>
<tr>
<th>Fruits</th>
<th>Type</th>
<th>color</th>
</tr>
</thead>
<tbody>
<?php if(!isset($result)){ ?>
<tr>
<td>Empty</td>
<td>Empty</td>
<td>Empty</td>
<td>Empty</td>
</tr>
<?php else{
foreach($result as $apples){ ?>
<tr>
<td></td>
<td><?php echo "$apples["type"]";?></td>
<td><?php echo "$apples["color"]";?></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
</table>
Upvotes: 0
Views: 336
Reputation: 21661
Well just at a glance this is completely wrong syntax
<?php echo "apples["type"]";?>
Probably you need something like this
<?php echo $apples["type"];?>
What you had will probably give you a syntax error because PHP will see it like this
<?php echo "apples[" type "]"; ?>
Where "apples["
is a complete string, type
is an undefined constant that is unexpected in the location it's in, and then an extra string "]";
you can test it in this sandbox
Which gives us this
Parse error: syntax error, unexpected 'type' (T_STRING), expecting ',' or ';' in /in/nR8vF on line 5
UPDATE
Well there are a few other obvious problems, just with this
$output = json_decode(JsonData);
$result = $output['data'][0]['apple'];
I assume JsonData
is just a placeholder or are you missing the $
on that too?
Then as you don't have the second argument set to true, then data
will be object style. Personally I would do
$output = json_decode(JsonData, true);
And just use it as an array.
Others have covered the double "foreach" deal (for looping on fruit), so I won't re-hash that, I would have covered it but it was "Dinner" time and my wife gets a bit "Irritated" when I mess with code instead of coming up from the office (I have an office in the basement).
Upvotes: 1
Reputation: 1600
If you are looking to show all fruits then you will need something along the lines of:
$result =$output['data'];
Then update your foreach loop
foreach($result as $key=>$fruit){
foreach($fruit as $k=>$dets){?>
<tr>
<td></td>
<td><?php echo $dets["type"];?></td>
<td><?php echo $dets["color"];?></td>
</tr>
<?php }
} ?>
That's also not valid json
Upvotes: 0
Reputation: 923
$arr = json_decode($json, true);
foreach($arr['data'] as $fruit => $types){
foreach($types as $info){
echo $fruit;
echo $info['type'];
echo $info['color'];
}
}
This is how you can get your JSON data into PHP. I trust you can create an HTML table or something to display this information.
Upvotes: 1