首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php将mysql数据解析成多维json数组

问php将mysql数据解析成多维json数组
EN

Stack Overflow用户
提问于 2014-10-03 17:15:42
回答 1查看 401关注 0票数 0

我是php webservices的新手。我希望获得以下格式的sql数据:

​

我使用的SQL查询是:

代码语言:javascript
复制
    $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`");

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

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

代码语言:javascript
复制
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;
        }

我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2014-10-03 17:55:06

我认为您正在尝试同时使用两个API,尝试将$result->fetch_array($result) (mysqli)改为mysql_fetch_array($result) (mysql)。

考虑停止使用mysql_函数,因为它们已被弃用,更多的here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26176331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档