2009-11-18 41 views
0

我正在使用在stackoverflow上找到的此解決方案將我的MYSQL輸出編碼爲JSON編碼數組。將項目添加到JSON編碼數組

$sth = mysql_query("SELECT ..."); 

$rows = array(); 

    while($r = mysql_fetch_assoc($sth)) { 
     $rows[] = $r; 
    } 

print json_encode($rows); 

這個偉大的工程和生產的

[{"id":"81","title":"Something Here","start":"2009-10-27 09:00:00"},{"id":"77","title":"Report on water","start":"2009-10-30 09:00:00"}] 

輸出現在我需要把一個值的說

"colour":"Blue" 

的JSON編碼數組內。

所以我需要的輸出中看起來像

[{"id":"81","title":"Community Awareness","start":"2009-10-27 09:00:00", "colour":"Blue"},{"id":"77","title":"Write a 10,000 Page Report on Emma","start":"2009-10-30 09:00:00", "colour":"Blue"}] 

有沒有人對我怎麼能做到這一點的任何解決方案?

感謝,

蒂姆·莫爾

回答

5

致電前json_encode($行),只需編輯$行數組中的值:其實

$rows[0]['colour'] = 'Blue'; // changes the colour of the first row in the array 

編輯,如果你只想添加一個顏色到所有的行,你可以做一個簡單的foreach:

foreach ($rows as &$row) { 
    $row['colour'] = 'Blue'; 
} 
+0

謝謝!就是這樣 – Tim 2009-11-18 04:52:16