2

iTunes の曲データを MySQL データベースにロードし、PHP を使用してアルバム情報の JSON フィードを取得しようとしています。このコード..

        <?php

    /* Connect  to database */
    require ('include/connect.php');

    /* Build the query */
            $query = "SELECT a.album,a.name,a.artist,a.year AS track_year, b.year AS album_year FROM staging a, (SELECT album, MAX(year) AS year FROM staging GROUP BY album) b WHERE a.album = b.album";

    /* Loop through the results and build a JSON array for the data table */
    $result = $mysqli->query($query);
    $info = array();
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

                if (!isset($info[$row['album']])) {
                    $info[$row['album']] = array(
                        'record' => $row['album']
                        , 'artist' => $row['artist']
                        , 'year' => $row['album_year']
                        , 'tracks' => array()
              );
           }

            $info[$row['album']]['tracks'][] = $row['name'];
    }       

    $data = json_encode($info);
            ?>

この結果が生成されます (2 つのアルバムに切り捨てられます)...

{
"Abbey Road": {
    "album": "Abbey Road",
    "artist": "The Beatles",
    "year": "1969",
    "tracks": [
        "Come Together",
        "Something",
        "Maxwell's Silver Hammer",
        "Oh! Darling",
        "Octopus's Garden",
        "I Want You (She's So Heavy)",
        "Here Comes The Sun",
        "Because",
        "You Never Give Me Your Money",
        "Sun King",
        "Mean Mr. Mustard",
        "Polythene Pam",
        "She Came In Through The Bathroom Window",
        "Golden Slumbers",
        "Carry That Weight",
        "The End",
        "Her Majesty"
    ]
},
"Accelerate": {
    "album": "Accelerate",
    "artist": "R.E.M.",
    "year": "2008",
    "tracks": [
        "Living Well Is the Best Revenge",
        "Man-Sized Wreath",
        "Supernatural Superserious",
        "Hollow Man",
        "Houston",
        "Accelerate",
        "Until the Day Is Done",
        "Mr. Richards",
        "Sing for the Submarine",
        "Horse to Water",
        "I'm Gonna DJ",
        "Supernatural Superserious (Live)"
    ]
}
}

私の結果がこのように見えることを望みます...

[
      {
        "album": "Abbey Road",
        "artist": "The Beatles",
        "year": "1969",
        "tracks": [
            "Come Together",
            "Something",
            "Maxwell's Silver Hammer",
            "Oh! Darling",
            "Octopus's Garden",
            "I Want You (She's So Heavy)",
            "Here Comes The Sun",
            "Because",
            "You Never Give Me Your Money",
            "Sun King",
            "Mean Mr. Mustard",
            "Polythene Pam",
            "She Came In Through The Bathroom Window",
            "Golden Slumbers",
            "Carry That Weight",
            "The End",
            "Her Majesty"
        ]
    },
     {
        "album": "Accelerate",
        "artist": "R.E.M.",
        "year": "2008",
        "tracks": [
            "Living Well Is the Best Revenge",
            "Man-Sized Wreath",
            "Supernatural Superserious",
            "Hollow Man",
            "Houston",
            "Accelerate",
            "Until the Day Is Done",
            "Mr. Richards",
            "Sing for the Submarine",
            "Horse to Water",
            "I'm Gonna DJ",
            "Supernatural Superserious (Live)"
        ]
    }
    ]

明らかに、私は PHP で JSON をエンコードするのは初めてです。私は何を間違っていますか?ありがとう!

4

1 に答える 1

2

配列を呼び出しarray_valuesます。

$data = json_encode(array_values($info));

これにより、名前付きインデックスが削除され、代わりに数値インデックスに変換されるjson_encodeため、配列をインデックス付き (連想配列ではなく) 配列として扱う必要があります。

于 2013-08-27T20:32:50.627 に答える