我想讓Devbridge Autocomplete jQuery腳本正常工作,而且我非常接近。我可以讓它給我建議(下拉值),但是我需要使用它的數據屬性。以PHP格式化JSON輸出MYSQL
建議的JSON格式爲如下:
{
suggestions: [
{ value: "United Arab Emirates", data: "AE" },
{ value: "United Kingdom", data: "UK" },
{ value: "United States", data: "US" }
]
}
到目前爲止,我已設法這樣的:
{
"suggestions": [
"Show Name 1",
"Show Name 2"
],
"data": [
"1",
"2"
]
}
生產的是輸出的代碼如下:
$reply = array();
$reply['suggestions'] = array();
$reply['data'] = array();
while ($row = $result->fetch_array(MYSQLI_ASSOC))//loop through the retrieved values
{
//Add this row to the reply
$reply['suggestions'][]=$row['SHOW_NAME'];
$reply['data'][]=$row['SHOW_ID'];
}
//format the array into json data
echo json_encode($reply);
有什麼建議麼?我無法弄清楚如何將兩個數據元素組合成一個陣列,更不用說前面加上他們與「價值」或「數據」 ......
看看這個問題:http://stackoverflow.com/questions/6054033/pretty-printing-json-with-php –