繁体   English   中英

PHP将MySQL数据解析为多维JSON数组

[英]php parse mysql data into multi dimensional json array

我是php webservices的新手。 我希望以以下格式获取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`");

通过此查询,我可以为每个唯一的offer获得多余的restaurant名称,这很好。 但是,如何将图像中给出的数据格式化为多维数组?

以下是到目前为止我尝试过的代码:

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_函数,因为它们已被弃用,请mysql_ 此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM