我是 php web 服务的新手。我希望获取以下格式的sql数据:
正在使用的 SQL 查询是:
$result = mysql_query("select r.id,r.Name, o.`Title`, o.id, o.`Description`, o.`OfferLocation`
from `restaurants` r left join `offers` o on r.id = o.`restaurant_id`");
通过此查询,我为每个唯一的优惠
获得了冗余的餐厅
名称,这很好。但是如何将图像中给出的数据格式化为多维数组?
以下是我迄今为止尝试过的代码:
if(mysql_num_rows($result) > 0){
$response["restaurant"] = array();$cnt=0;
$item_rest=array();
while($row=mysql_fetch_array($result)){
$item_rest["RestaurantID"]=$row["id"];
}
$response_offer["offers"]=array();
while($row=$result->fetch_array($result)){
$item = array();
$item["Offer"]= $row["Title"];
$item["OfferId"]= $row["id"];
$item["Description"]= $row["Description"];
$item["OfferLocation"]= $row["OfferLocation"];
array_push($response_offer["offers"],$item);
}
array_push($response["restaurant"],$response_offer);
$response["success"]=1;
}
我该怎么做?
最佳答案
我认为您正在尝试同时使用两个 API,请尝试将 $result->fetch_array($result)
(mysqli) 更改为 mysql_fetch_array($result)
(mysql)。
考虑停止使用 mysql_
函数,因为它们已被弃用,更多 here
关于php将mysql数据解析为多维json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26176331/