php查询多个数组json

时间:2014-09-24 04:30:43

标签: php mysql json

我正在尝试用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);
   ?>

亲吻^^

1 个答案:

答案 0 :(得分:0)

我想您是在询问如何在while块中格式化数组以实现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);
?>