2013-08-27 80 views
2

我已经将我的iTunes歌曲数据加载到MySQL数据库中,并且我试图使用PHP来获取专辑信息的JSON订阅源。此代码..来自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); 
      ?> 

产生这样的结果(截断为两张专辑)......

{ 
"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的新手。我究竟做错了什么? 谢谢!阵列上

+0

你能帮我解决这个问题吗 http://stackoverflow.com/questions/32312441/json-array-from-php-file-and-database-server-in-real-time – hawkeye

回答

2

呼叫array_values

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

这将删除您命名的索引,并将其转换为数字,而不是那些,所以json_encode应该把你的数组作为索引(而不是关联)阵列。