1

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.

2
  • foreach ($item['images'] as $v) Commented Dec 13, 2016 at 19:39
  • Thank you. What I am trying to do is to ONLY return the "url" value under the coverType: banner. I want to ignore the others. I was Guessing it would be something like: $myurl = $item['images']['coveType:banner']'[url']; Commented Dec 13, 2016 at 21:03

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Stuart. As I describe in my comment above, I only want to get the value for the url that is under coverType:banner. Basically i want a variable that will return me the value that in my example would be "another_random_file_here.jpg". I have no idea what this value will be as they will vary greatly. I hope this makes sense.
Sorry Stuart. Not a big expert... My json string will contain may entries, I have created a more expanded sample of the JSON output. See Pastebin. Not sure how to create my code so I can get a "title" for each entry, a banner "url" for each entry. I just do not understand how your answer will relate to the Title and other data I need to scrape for each of the array entries. A million thanks for your help.
I'll update my answer. Your JSON is not valid either - there are missing braces to close each series...
Thank you Stuart.... This was the answer. I am very grateful for your help. I have been trying for hours. The invalid JSON is probably because I I simplified the code for clarity and I am sure I deleted a few things. Many, many thanks!
0

Suppose this is the variable jsondecoded, $json[images][1]->coverType;

2 Comments

Can you elaborate on this answer? I'm missing if it's another question or an answer.
'$json=$json_decode($variablenam); ' ' $json[images][1]->coverType; ' see in json you acces each by objects like u could have did images->covertype but as u can see the covertype data is in array so for that we use image[array pos postion of covertype]->covertype;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.