Ivo Ivo - 1 month ago 12
JSON Question

How do I echo objects from nested arrays?

I'm using json_decode, how do I echo all the "owner"s of this https://www.brick-hill.com/API/serials?id=20

<?php

$itemOwners = json_decode(file_get_contents("https://www.brick-hill.com//API//serials?id=1937"));
echo $itemOwners->owner;

?>

Answer Source

$itemOwners is an array. You need to iterate over it using foreach:

<?php

$itemOwners = json_decode(file_get_contents("https://www.brick-hill.com//API//serials?id=1937"));

foreach ( $itemOwners as $owner ) {
    echo $owner->owner;
}

?>

Also, be aware, this is not valid json. See here. Seems you have a comma at the end (Before the closing ] which is causing a syntax error)