I’m a php newb and having some problems understanding how to parse a json api return. Here is a snippet of the json:
{
"results": [
{
"list_name": "",
"display_name": "",
"updated": "",
"bestsellers_date": "",
"published_date": "",
"isbns": [
{
"isbn10": "",
"isbn13": ""
},
{
"isbn10": "",
"isbn13": ""
}
],
"book_details": [
{
"title": "Book author",
"description": "Book description"",
"contributor": "",
"author": "Book author",
"contributor_note": "",
"price": ,
"age_group": "",
"publisher": "",
"primary_isbn13": "1010101010",
"primary_isbn10": "1010101010101"
}
],
"reviews": [
{
"book_review_link": "",
"first_chapter_link": "",
"sunday_review_link": "",
"article_chapter_link": ""
}
]
},
And here’s the php I have so far…
<?php
$json=file_get_contents('the url for the api return');
// create array
$json_a=json_decode($json,true);
foreach($json_a[results] as $r)
{
echo '
List: '.$r[list_name].'
<br />
Title: '.$r[title].'
<br>
Description: '.$r[description].'
<br />
<br />
';
}
?>
I’m getting a return from the api and it’s providing the correct value for ‘list_name’, but ‘title’ and ‘description’ remain unpopulated; so, I know I’m not accessing those subkeys(?) correctly. What is the correct syntax for getting at those elements?
Really appreciate any guidance y’all can provide on this. Thanks in advance.
title and description are inside a sub-element “book_details”, which is an array itself
should be
and
Just turn your error reporting on in PHP, should become obvious that the indexes you’re trying to access don’t exist..
update: added the array index