php将mysql数据解析为多维json数组

标签 php mysql sql arrays json

我是 php web 服务的新手。我希望获取以下格式的sql数据: enter image description here

正在使用的 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/

相关文章:

sql - 选择两列值之间的记录

mysql - 使用 'datetime' 列选择最近 3 个月的记录的简单方法

javascript - 使用 PHP 获取 Web 服务数据

php - 表单提交后,滚动到表单

php - 将字符串转换为日期和日期时间

mysql - 使用字段数据的条件 JOIN

php - 将时间戳添加到数据库表

python - Insert ... ON DUPLICATE KEY INSERT something new 内容

mysql - Laravel 家园 : Base table not found

mysql - 在sql中,如何根据另一列的值制作排名列?