Parse multi level json data with php -
{ "books": [ { "id": 2331, "image": "http://lol.org/flower.png", "images": [ { "256x144": "http://lol.org/bee.png", "650x320": "http://lol.org/fly.png" } ], ....
i have json data above problem how out 650x320 data.
$data = json_decode($jsondata,true); $gg = sizeof($data['books']); for($x=0;$x<$gg;$x++){
codes below works fine
$image = $data['books'][$x]['image'];
but how fetch images on second json level? have tried code below no luck.
$image = ($data->{'books'}->{'images'}->{'320x180'}); $image = $data['books']['images'][$x]['320x180'];
'books'
array of objects, need select object using numeric index.
$image = $data['books'][$insertindexhere]['images'][$insertindexhere]['320x180'];
essentially have missed [$x]
between 'books'
, 'images'
first code works.
you want loop iterates through each book , second nested loop iterates through images in each book.
for example:
$gg = sizeof($data['books']); for($x=0;$x<$gg;$x++) { $images = data['books'][$x]['images']; $sizeofimages = sizeof($images); for($j = 0; $j < $sizeofimages; $j++) { // access $images[$j]['320x180'] } }
Comments
Post a Comment