如何使用laravel或mysql创建嵌套json?

问题描述 投票:0回答:1

我需要这样的东西:

  "results":[  
  {  
     "id":1,
     "date":{  
        "date":"2015-04-1 00:00:00.000000",
        "timezone_type":3,
        "timezone":"America\/Denver"
     }
  },

查询:

$find = DB::select( DB::raw("SELECT created_at as date, 'America/Denver' as timezone, 3 as timezone_type FROM table"));
return Response::json($find);

如何使用 mysql/Laravel 创建日期内的日期?我尝试使用 array_merge 但日期将附加在底部而不是使其自身嵌套。

php mysql json laravel nested
1个回答
3
投票

只需将您的代码更改为以下代码即可:

        $tableIds = DB::select( DB::raw("SELECT id FROM table"));
        $jsonResult = array();

        for($i = 0;$i < count($tableIds);$i++)
        {
            $jsonResult[$i]["id"] = $tableIds[$i]->id;
            $id = $tableIds[$i]->id;
            $jsonResult[$i]["date"] = DB::select( DB::raw("SELECT created_at as date, 'America/Denver' as timezone, 3 as timezone_type  FROM table WHERE id = $id"));
        }

        return Response::json(array(
                    'error'     =>  false,
                    'stores'    =>  $jsonResult),
                    200
            );
© www.soinside.com 2019 - 2024. All rights reserved.