在Php中构建具有多个参数的Json数组
我设法在Json数组中输出列'resort',但我还需要'country'和'aantal'。我不知道怎么做。有人能帮我吗在Php中构建具有多个参数的Json数组,php,arrays,json,Php,Arrays,Json,我设法在Json数组中输出列'resort',但我还需要'country'和'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_i
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";
}
if($numrows<1&&strlen($sq)>3)
{
$sql=“从sx\U cam选择度假村、国家/地区、计数(图像)作为安塔尔
左连接sv_orte ON sv_cam.res_id=sv_orte.res_id
其中,sound=soundex(“$sq”)和(状态<1)由aantal desc按度假村顺序分组;
$result2=mysql\u query($sql)或die(mysql\u error());
$numrows=mysql\u num\u行($result2);
$建议=2;
}
$items=array();
而($row=mysql\u fetch\u assoc($result2)){
$items[$row['resort']]=$row['resort'];
}
foreach($key=>$value的项目){
echo strtolower($key)。“|$value\n”;
}
您以错误的方式构建阵列。一旦你得到了正确的数组,就只需调用
我不完全确定您希望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的结构,以满足您的需要。
$items[$row['resort']]=$row['resort']代码>为什么为数组中的每个元素指定相同的键和值?这有什么用?一个是小写的,另一个是正常的。这毫无意义;我同意