我设法在Json数组中输出了列"resort",但我也需要"country"answers"aantal"。不知道怎么做。有人能帮帮我吗?
if ($numrows < 1 && strlen($sq) > 3)
{
$sql = "SELECT resort, country, COUNT(image) AS aantal FROM sx_cam
LEFT JOIN sv_orte ON sv_cam.res_id = sv_orte.res_id
WHERE sound=soundex('$sq') and (status < 1) GROUP BY resort order by aantal desc";
$result2 = mysql_query($sql) or die(mysql_error());
$numrows = mysql_num_rows($result2);
$suggest = 2;
}
$items = array();
while($row = mysql_fetch_assoc($result2)){
$items[$row['resort']] = $row['resort'];
}
foreach ($items as $key=>$value) {
echo strtolower($key)."|$value'n";
}
您构建数组的方式不对。一旦你得到了正确的数组,它就像调用json_encode
一样简单
我不完全确定你希望json看起来怎么样,但这样的东西应该会让你开始
$items = array();
while($row = mysql_fetch_assoc($result2)){
//first we build an 'object' of the current result
$item['country'] = $row['country'];
$item['resort'] = $row['resort'];
//now push it on the array of results
$items[] = $item;
}
echo json_encode($items);
一旦以上代码正常工作,就可以调整PHP数组来更改JSON的结构,以满足您的需求。