-1

I have a json file (Rather large, mind you, the xml version of the same file is over 19000 lines long) that goes something like this:

object(stdClass)#1 (1) {
    ["result"]=> object(stdClass)#2 (13) {
        ["items"]=> array(1628) {
            [0]=> object(stdClass)#27 (18) {
                ["image_url"]=> string(95) "http://media.steampowered.com/apps/440/icons/c_bat.d037d6a40ec30ab4aa009387d476dca889b6f7dc.png"
            }
            [1]=> object(stdClass)#29 (18) {
                ["image_url"]=> string(98) "http://media.steampowered.com/apps/440/icons/w_bottle.859ddb315a2748f04bcc211aa7a04f2c926e6169.png"
            }
        }
    }
}

I want to get the image url of each object in the 'items' array, and I tried to do so with this:

<?PHP

$api = "http://api.steampowered.com/IEconItems_440/GetSchema/v0001/?key=yoink&language=en_US&format=json";

$json = file_get_contents($api);

$schema = json_decode($json);

print count($schema->result->items);

foreach($schema->results->items as $item) {
    print "{$item->image_url}";
} 

?>

But when I check the page it prints out, I get this:

1628 Warning: Invalid argument supplied for foreach() in - on line 13

Now I know this array is definitely not empty as count returns 1628, and well it isn't null either because I had got a var_dump earlier, as seen above. Can anyone help me as to where I went wrong?

EDIT: I need to learn how to read. Please vote to close this question!

Luke
  • 4,908
  • 1
  • 37
  • 59
  • Does each entry always have an image_url? If one was missing, then you'd get that error, I suspect. – andrewsi Jun 19 '13 at 18:03

2 Answers2

1

Typo, change:

foreach($schema->results->items as $item) {
                       ^-- here

to:

foreach($schema->result->items as $item) {
Sam
  • 20,096
  • 2
  • 45
  • 71
0

Typo. It's not results, it's result.

foreach($schema->result->items as $item) {
    print "{$item->image_url}";
} 
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174