2012-07-21 83 views
1

我在這裏看到了很多類似的問題,但我一直無法得到我想要的結果。有人可以幫我嗎?如何生成以下JSON結構?我的查詢很好,我只是不知道如何循環,並得到這個。由於對PHP的JSON MySQL結果

所需的輸出,其與產生

{ 
    "players": [ 
     { 
      "id": 271, 
      "fname": "Carlos", 
      "lname": "Beltran", 
      "position": "OF", 
      "stats": [ 
       { 
        "year": "2010", 
        "hr": 32, 
        "rbi": 99, 
        "team": "NYM" 
       }, 
       { 
        "year": "2011", 
        "hr": 35, 
        "rbi": 100, 
        "team": "STL" 
       }, 
       { 
       ............ 
       } 
      ] 
     }, 
     { 
      ........ 
     } 
    ] 
} 

電流輸出

{"0":{"cbs_id":"18817","fname":"Carlos","lname":"Beltran"},"stats":[{"year":"2007","hr":"33","rbi":"112"}]} 

{"0":{"cbs_id":"174661","fname":"Willie","lname":"Bloomquist"},"stats":[{"year":"2007","hr":"2","rbi":"13"}]} 

{"0":{"cbs_id":"1208693","fname":"Brennan","lname":"Boesch"},"stats":[{"year":"2010","hr":"14","rbi":"67"}]} 

:(我知道我的路要走)

if ($result = mysqli_query($link, $sql)) { 

     $player = array(); 

     while ($row = $result->fetch_assoc()) 
     { 

      $player[] = array (
      'cbs_id' => $row['cbs_id'], 
      'fname' => $row['fname'], 
      'lname' => $row['lname'] 
      ); 

      $player['stats'][] = array(
       'year' => $row['year'], 
       'hr' => $row['hr'], 
       'rbi' => $row['rbi'] 
      ); 

     } 

     $json = json_encode($player); 
     echo "<pre>$json</pre>"; 

     mysqli_free_result($result); 
    } 
} 

注意:每個玩家可以有更多的一個「統計」記錄(年,hr,rbi等)

+3

請不要在新代碼中使用'mysql_ *'函數。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-21 20:33:17

+0

@Truth感謝您的信息 – relyt 2012-07-21 20:43:31

回答

3

這可能給你想要的東西:

$players = array(); 

while ($row = $result->fetch_assoc()) 
{ 
    $id = (int)$row['cbs_id']; 

    if (! isset($players[$id])) 
    { 
    // New player, add to $players array. 
    // For the moment index players by ID so stats can be easily added 
    // to an existing player. Without indexing (using $players[] = ...), 
    // the same player would be added for each stats record related to 
    // him. 
    $players[$id] = array(
     'id'  => $id, 
     'fname' => $row['fname'], 
     'lname' => $row['lname'], 
     'stats' => array() 
    ); 
    } 

    // Add the stats 
    $players[$id]['stats'][] = array(
    'year' => (int)$row['year'], 
    'hr' => (int)$row['hr'], 
    'rbi' => (int)$row['rbi'] 
); 
} 

// Players are indexed by their ID in $players but need to be contained in 
// a JSON array, so use array_values() to remove indices, e.g. convert 
// 
//  array(
//  271 => array('id' => 271, ...), 
//  ... 
// ) 
// 
// to 
// 
//  array(
//  array('id' => 271, ...), 
//  ... 
// ) 
// 
$data = array('players' => array_values($players)); 
$json = json_encode($data); 

最終,你可能省去了整型轉換,讓PHP自動轉換字符串化的數值數據(這是MySQL查詢結果的默認行爲s)返回到PHP編號通過使用MYSQLI_OPT_INT_AND_FLOAT_NATIVE mysqli連接選項,如this page的示例#5中所述。請注意,它需要PHP使用mysqlnd庫(您可以使用phpinfo來檢查)。

2

您需要一個關聯數組。像這樣的東西應該讓你開始

$data = array(
    'players' => array(
     array(
      'id' => 271, 
      'fname': 'Carlos', 
      'lname': 'Beltran', 
      'position': 'OF', 
      'stats' => array(
       array(
        'year' => 2010, 
        'hr': 32, 
        'rbi': 99, 
        'team': 'NYM' 
       ), 
       ... 
      ) 
     ), 
     ... 
    ) 
); 

要編碼成JSON,使用json_encode

$json = json_encode($data); 
+0

我想問題是我的問題是正確構建陣列 – relyt 2012-07-21 19:52:57

+0

@relyt你有什麼嘗試? – alexn 2012-07-21 19:54:20

+0

我更新了我的問題。我知道我走了,但我一直盯着這個太久了。 – relyt 2012-07-21 20:26:58