0

I'm taking some json, made by OpenLibrary.org, and remake a new array from the info. Link to the OpenLibrary json

here is my PHP code to decode the json:

$barcode = "9781599953540";

function parseInfo($barcode) {
    $url = "http://openlibrary.org/api/books?bibkeys=ISBN:" . $barcode . "&jscmd=data&format=json";
    $contents = file_get_contents($url); 
    $json = json_decode($contents, true);
    return $json;
}

the new array I'm trying to make looks something like this:

$newJsonArray = array($barcode, $isbn13, $isbn10, $openLibrary, $title, $subTitle, $publishData, $pagination, $author0, $author1, $author2, $author3, $imageLarge, $imageMedium, $imageSmall);

but when I try to get the ISBN_13 to save it to $isbn13, I get an error:

Notice: Undefined offset: 0 in ... on line 38 
// Line 38
$isbn13 = $array[0]['identifiers']['isbn_13'];

And even if I try $array[1] ,[2], [3].... I get the same thing. What am I doning wrong here! O I know my Valuable names might not be the same, that's because they are in different functions.

Thanks for your help.

6
  • 1
    What is contained in $array? var_dump($array). In your code above, you populate $json, not $array. Commented Jul 15, 2012 at 14:11
  • The array is probably indexed by string keys, not integers. also, where does $array show up? Commented Jul 15, 2012 at 14:12
  • 1
    @GuyDavid Or if its origin is JSON, they may be object properties. Commented Jul 15, 2012 at 14:13
  • @Michael I know my Valuable names might not be the same, that's because they are in different functions. $json is $array. Commented Jul 15, 2012 at 14:13
  • @Throdne very well then, please var_dump($json) or var_dump($array) so we can see what's in it! Commented Jul 15, 2012 at 14:13

1 Answer 1

2

Your array is not indexed by integers, it is indexed by ISBN numbers:

Array
(
    // This is the first level of array key!
    [ISBN:9781599953540] => Array
        (
            [publishers] => Array
                (
                    [0] => Array
                        (
                            [name] => Center Street
                        )

                )

            [pagination] => 376 p.
            [subtitle] => the books of mortals
            [title] => Forbidden
            [url] => http://openlibrary.org/books/OL24997280M/Forbidden
            [identifiers] => Array
            (
                [isbn_13] => Array
                    (
                        [0] => 9781599953540
                    )

                [openlibrary] => Array
                    (
                        [0] => OL24997280M
                    )

So, you need to call it by the first ISBN, and the key isbn_13 is itself an array which you must access by element:

// Gets the first isbn_13 for this item:
$isbn13 = $array['ISBN:9781599953540']['identifiers']['isbn_13'][0];

Or if you need a loop over many of them:

foreach ($array as $isbn => $values) {
  $current_isbn13 = $values['identifiers']['isbn_13'][0];
}

If you expect only one each time and must be able to get its key without knowing it ahead of time but don't want a loop, you can use array_keys():

// Get all ISBN keys:
$isbn_keys = array_keys($array);
// Pull the first one:
$your_item = $isbn_keys[0];
// And use it as your index to $array
$isbn13 = $array[$your_item]['identifiers']['isbn_13'][0];

If you have PHP 5.4, you can skip a step via array dereferencing!:

// PHP >= 5.4 only
$your_item = array_keys($array)[0];
$isbn13 = $array[$your_item]['identifiers']['isbn_13'][0];
Sign up to request clarification or add additional context in comments.

3 Comments

I see, But the problem is anytime $barcode changes, the ['ISBN:9781599953540'] will change also. I need to make my code dynamic. Is there anyway of doing it with array_keys(). Sorry, very new to PHP. Still learning.
@Throdne do you only expect one item back each time, or many?
@Throdne see addition above for array_keys() usage here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.