Nested JSON PHP decode <p>I have the following Json file </p> <pre><code>{ "username”: “userabc”, “locations”: [ { “locationId": "2123", “locationName": "Test Site", “setupDate”: "0000-00-00", “dataType”: { “book”: [ { “bookId": “1257245", “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" }, { “bookId": “4257245", “information”: “Infotag 11”, “addedDate": "0000-00-00 00:00:00" }, { “bookId": “2227242”, “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" } ], “tape”: [ { “tapeId": “1220”, “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" }, { “tapeId": “1320”, “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" } ], “record”: [ { “recordId": “a21322”, “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" }, { “recordId": “b213222”, “information”: “Infotag 181”, “addedDate": "0000-00-00 00:00:00" } ], "virtual": [ { “virtId": "2123", “information”: "57235", “addedDate”: "0000-00-00 00:00:00", } ] } } ] } </code></pre> <p>I'm currently trying to access the nested part of (to count the number of bookIDs). I have done the following, can print the locationName but I am unable to print the bookId, I'm not sure where I'm going wrong.</p> <pre><code>$obj = json_decode($json,true); foreach($obj['locations'] as $chunk) { $locName = $chunk['locationName']; echo $locName; } </code></pre> <p>This is the part I am having problems with, I am not able to see any results?</p> <pre><code>foreach($obj['locations']['book'] as $chunk) { $bkId = $chunk['bookId']; echo $bkId; } </code></pre>