0

我正在尝试让Devbridge Autocomplete jQuery脚本工作,而且我非常接近。我可以得到它来给我建议(下拉值),但是我需要使用它的数据属性。

建议的 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);

有什么建议么?我不知道如何将两个数据元素组合成一个数组,更不用说在它们前面加上“值”或“数据”了......

4

3 回答 3

1
$response = array();
$reply = array();
while ($row = $result->fetch_array(MYSQLI_NUM))//loop through the retrieved values
{
    //Add this row to the reply
    $reply['value'] = $row[0];
    $reply['data'] = $row[1];
    $response['suggestions'][] = $reply;
}
//format the array into json data
echo json_encode($response, JSON_PRETTY_PRINT);
于 2013-07-22T01:59:17.323 回答
1
while($row = $result->fetch_array(MYSQLI_ASSOC))
{

$rec = array();

$rec['value'] = $row['SHOW_NAME'];
$rec['data'] = $row['SHOW_ID'];

$payload['suggestions'][] = $rec;

}

echo json_encode($payload);
于 2013-07-22T02:00:25.317 回答
0

不确定我是否正确理解了您,但如果您的意思是在一个数组中获取两个值,请使用:

$replay[][array('country' => $row['SHOW_NAME'],'data' => $row['SHOW_ID'])];
于 2013-07-22T02:00:49.343 回答