0

OpenLibrary.orgによって作成されたjsonを取得し、情報から新しい配列を再作成します。 OpenLibraryjsonへのリンク

これがjsonをデコードするための私のPHPコードです:

$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;
}

私が作成しようとしている新しい配列は、次のようになります。

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

しかし、ISBN_13を取得して$ isbn13に保存しようとすると、エラーが発生します。

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

そして、$ array [1]、[2]、[3]....を試しても同じことがわかります。私はここで何を間違っているのですか!O私は私の貴重な名前が同じではないかもしれないことを知っています、それはそれらが異なる機能にあるからです。

ご協力いただきありがとうございます。

4

1 に答える 1

2

配列は整数でインデックス付けされておらず、ISBN番号でインデックス付けされています。

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
                    )

したがって、最初のISBNで呼び出す必要があり、キーisbn_13自体が配列であり、要素ごとにアクセスする必要があります。

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

または、それらの多くをループする必要がある場合:

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

毎回1つだけを期待し、事前に知らなくてもキーを取得できなければならないが、ループが必要ない場合は、次を使用できます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];

PHP 5.4を使用している場合は、配列の逆参照を介して手順をスキップできます!:

// PHP >= 5.4 only
$your_item = array_keys($array)[0];
$isbn13 = $array[$your_item]['identifiers']['isbn_13'][0];
于 2012-07-15T14:17:30.393 に答える