Reputation: 135
I am trying to decode a portion of a json code in PHP. The json looks something like:
"title":"A Title Here",
"images":[
{
"coverType":"fanart",
"url":"some_random_file_here.jpg"
},
{
"coverType":"banner",
"url":"another_random_file_here.jpg"
},
{
"coverType":"poster",
"url":"yet_another_random_file_here.jpg"
}
],
I want to get the url that is under the "coverType":"banner"
I can easily parse the title with the following code:
$itemNr = 0;
foreach($json as $item) {
$mytitle = $item['title'];
echo $mytitle;
$itemNr++;
How would my code look like using the same concept. Please note that I have simplified the code of JSON. The actual php for some items not shown on my code above look something like:
$somevariable = $item['series']['tvdbId'];
Any advice is greatly appreciated.
Thanks,
H.
Upvotes: 1
Views: 17830
Reputation: 5
Suppose this is the variable jsondecoded, $json[images][1]->coverType;
Upvotes: 0
Reputation: 6795
If you can access the images key, then:
<?php
$json = <<<JSON
{
"title":"A Title Here",
"images":[
{
"coverType":"fanart",
"url":"some_random_file_here.jpg"
},
{
"coverType":"banner",
"url":"another_random_file_here.jpg"
},
{
"coverType":"poster",
"url":"yet_another_random_file_here.jpg"
}
]
}
JSON;
$json = json_decode($json);
print_r($json);
foreach ($json->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
?>
Gives:
Image Cover Type: banner
URL: another_random_file_here.jpg
UPDATE:
The JSON file you link to seems invalid, missing braces after each series. Here's the corrected JSON, and code:
<?php
$json = <<<JSON
[
{
"series": {
"title": "Brooklyn Nine-Nine",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/269586-15.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/269586-g3.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/269586-13.jpg"
}
],
"year": 2013
}
},
{
"series": {
"title": "The Middle",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/95021-16.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/95021-g14.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/95021-8.jpg"
}
],
"year": 2009
}
},
{
"series": {
"title": "New Girl",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/248682-43.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/248682-g20.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/248682-14.jpg"
}
],
"year": 2011
}
}
]
JSON;
$json = json_decode($json);
// echo '<pre>' .print_r($json, 1) .'</pre>';
foreach ($json as $item)
{
echo 'Title: ' .$item->series->title .'<br/>';
foreach ($item->series->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
}
?>
Gives:
Title: Brooklyn Nine-Nine Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/269586-g3.jpg
Title: The Middle Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/95021-g14.jpg
Title: New Girl Image Cover Type: banner URL: http://thetvdb.com/banners/graphical/248682-g20.jpg
Upvotes: 6