I'm not able to get the content of this array, please can someone help?

Where Am I getting it wrong?

Below is the code

$barcode = "9780980200447";

function parseInfo($barcode) {
    $url = "http://www.librarything.com/api_getdata.php?userid=timspalding&showstructure=1&max=10&showReviews=1&showTags=1&reviewsOnly=1&name=Jesus+is+Lord&&apikey=b27cb381baddff9c49b9dd27d9d96651&responseType=json";
    $contents = file_get_contents($url); 
    $json = json_decode($contents, true);
    return $json;
}

$array = (parseInfo($barcode));
//print_r($array);
foreach ($array as  $values) {
echo  $author_fl  = $values['show']['showCovers'];
echo  $book_id  = $values['books'];
echo  $title  = $values['settings']['title'];
  $covers = $values['settings']['covers']; // Author (last, first) 
  $author_lf = $values['settings']['author_lf']; // Author (last, first) 
  $author_fl = $values['settings']['author_fl'];  // Author (first, last) 
echo  $author_code  = $values['amazonchoice']['author_code'];
  $ISBN  = $values['amazonchoice']['ISBN'];
  $publicationdate  = $values['amazonchoice']['publicationdate'];
  $entry_stamp = $values['amazonchoice']['entry_stamp'];
  $entry_date = $values['amazonchoice']['entry_date'];
  $copies  = $values['amazonchoice']['copies'];
  $notes = $values['amazonchoice']['notes'];
  $language_main  = $values['amazonchoice']['language_main'];

}

Thanks.

The values you are trying to display have no values so you see nothing. To prove that add some static texts to the echo statement, like this:

foreach ($array as  $values) {

    echo  'AUTHOR FL: ' . $author_fl  = $values['show']['showCovers'];
    echo  'BOOK_ID: ' . $book_id  = $values['books'];
    echo  'TITLE: ' . $title  = $values['settings']['title'];
    $covers = $values['settings']['covers']; // Author (last, first)
    $author_lf = $values['settings']['author_lf']; // Author (last, first)
    $author_fl = $values['settings']['author_fl'];  // Author (first, last)
    echo  'AUTHOR_CODE: ' . $author_code  = $values['amazonchoice']['author_code'];
    $ISBN  = $values['amazonchoice']['ISBN'];
    $publicationdate  = $values['amazonchoice']['publicationdate'];
    $entry_stamp = $values['amazonchoice']['entry_stamp'];
    $entry_date = $values['amazonchoice']['entry_date'];
    $copies  = $values['amazonchoice']['copies'];
    $notes = $values['amazonchoice']['notes'];
    $language_main  = $values['amazonchoice']['language_main'];
}

You can always test json/arrays or other php functions online on Functions online, e.g. this is the link for json_decode.

In addition: $values['books'] is an array where the index key is the book_id, for example:

[books] => Array
    (
        [38] => Array
            (
                [book_id] => 38
                [title] => Alexander of Macedon, 356-323 B.C. : a historical biography.
                [author_lf] => Green, Peter
                [author_fl] => Peter Green
                [author_code] => greenpeter
                [ISBN] => 0520071654
                [ISBN_cleaned] => 0520071654
                [publicationdate] => 1991
                [entry_stamp] => 1124597764
                [entry_date] => Aug 21, 2005
                [copies] => 1
                [rating] => 5
                [language_main] => eng
                [language_secondary] => 
                [language_original] => 
                [hasreview] => 1
                [dateacquired_stamp] => 0
                [dateacquired_date] => Dec 31, 1969
                [cover] => http://pics.cdn.librarything.com/picsizes/44/87/448711e34ba0018597835424e67414141414141.jpg
                [bookreview] => This was my introduction to Alexander the Great. I still think it's the best, although Robin Lane Fox's...
                [bookreview_stamp] => 1229121379
                [bookreview_date] => Dec 12, 2008
                [tags] => Array
                    (
                        [0] => favorite
                        [1] => alexander the great
                    )

            )

So, to see the results you have to loop it:

foreach($values['books'] as $book_key => $book_value)
{
    echo $book_value['title'];
}
commented: Well spotted +11

That didnt work.

So where did you get stuck? I managed to access any element of the above array.

Please can see the exact codes you used?

Your array has two main elements:

  1. settings - an array of common settings
  2. books - an array of book data (each element here contains the same book data)

Therefore you have to use an if condition and check for the main key to access other values. I tested it with this code:

$array = (parseInfo($barcode));

// this is to format the output nicely
echo "<pre>";

// print_r($array);

foreach ($array as $key => $values) {

    /*
    echo  'AUTHOR FL: ' . $author_fl  = $values['show']['showCovers'];
    echo  'BOOK_ID: ' . $book_id  = $values['books'];
    echo  'TITLE: ' . $title  = $values['settings']['title'];
    $covers = $values['settings']['covers']; // Author (last, first)
    $author_lf = $values['settings']['author_lf']; // Author (last, first)
    $author_fl = $values['settings']['author_fl'];  // Author (first, last)
    echo  'AUTHOR_CODE: ' . $author_code  = $values['amazonchoice']['author_code'];
    $ISBN  = $values['amazonchoice']['ISBN'];
    $publicationdate  = $values['amazonchoice']['publicationdate'];
    $entry_stamp = $values['amazonchoice']['entry_stamp'];
    $entry_date = $values['amazonchoice']['entry_date'];
    $copies  = $values['amazonchoice']['copies'];
    $notes = $values['amazonchoice']['notes'];
    $language_main  = $values['amazonchoice']['language_main'];
    */

    // MY TEST

    echo "--------------<br> $key <br>--------------<br>";
    echo print_r($values, 1);

    if($key == 'settings') {
        echo '*** [show][showCovers] ***<br>' . print_r($values['show']['showCovers'], 1);
        echo '*** [settings][title] ***<br>' . print_r($values['title'], 1);
    } else {
        echo '*** [books] ***<br>' . print_r($values['books'], 1);
    }
}

echo "</pre>";

And as I said, some values are emty so nothing gets displayed.

commented: You could simply do `echo "<pre>" . print_r($array, true) . "</pre>";` -1

thanks so much. works now

You are welcome. Please mark as solved if no more questions. Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.