gpt4 book ai didi

php查询多个json数组

转载 作者:行者123 更新时间:2023-11-29 23:44:40 26 4
gpt4 key购买 nike

我正在尝试使用 php 搜索处理输入并发送到 json

json 输出如下:

{
"err": 0,
"msg": "",
"data": {
"f": 0,
"hotel": [
{
"att": 25147,
"name": "Crowne Plaza Changi Airport",
"city": "Singapore",
"country": "Singapore"
}
],
"city": [
{
"att": "-2679652",
"name": "Singapore",
"region": "",
"country": "Singapore",
"nr_hotels": ""
}
]
}
}

我尝试使用测试阵列但不起作用

<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);

// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){
$search_array = array(
"err"=>"","msg"=>"",
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']),
"city"=> array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country'])
);

array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>

我对此进行了更改,但仅显示 1 条记录,与关键搜索并不全部相似

$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);

// Do Search
$json_array = array();
while ($hasil=mysql_fetch_array($result)){

$search_array = array(
"err"=>intval("0"),"msg"=>"","data"=>array("f"=>intval("114"),
"hotel"=>array(
$hotel_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
)),
"city"=>array(

$city_array = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"city" => $hasil['city'],
))

));

}


echo json_encode($search_array);

?>

请帮助数组的语法,谢谢

与此网站类似的过程 http://www.myhotelfinder.com/id/home/dohttp/predict?q=singapore

谢谢你这个json输出我的意思

<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);

// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);

array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>

亲吻^^

最佳答案

我认为您是在问如何在 while block 中格式化数组才能实现 JSON 输出。

我认为你可以尝试以下数组声明:

<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);

// Do Search
$json_array = array();
while ($search=mysql_fetch_array($result)){

$search_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(
"att" => $search['hotel_id'],
"name" => $search['hotel_name'],
"city" => $search['city'],
"country" => $search['country']
),
"city" => array(
"att"=> $search['hotel_id'],
"name" => $search['city'],
"country" => $search['country']
)
)
);


array_push($json_array,$search_array);
}
echo json_encode($json_array);
?>

更新:好吧,我开始看到你可能会做的更多事情。请尝试以下操作:

<?
$query = 'SELECT * FROM hotel WHERE (hotel_name LIKE "%'.$q.'%" OR city LIKE "%'.$q.'%")';
$result=mysql_query($query);

// Do Search
$json_array = array(
"err" => "",
"msg" => "",
"data" => array(
"f" => 0,
"hotel" => array(),
"city" => array()
)
);
while ($hasil=mysql_fetch_array($result)){
$hotel = array(
"att" => $hasil['hotel_id'],
"name" => $hasil['hotel_name'],
"city" => $hasil['city'],
"country" => $hasil['country']
);
$city = array(
"att"=> $hasil['hotel_id'],
"name" => $hasil['city'],
"country" => $hasil['country']
);

array_push($json_array["data"]["hotel"], $hotel);
array_push($json_array["data"]["city"], $city);
}
echo json_encode($json_array);
?>

关于php查询多个json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26008580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com