2015-04-01 93 views
0

我需要有这样的事情:如何用laravel或mysql创建嵌套的json?

"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,但日期将被附加在底部而不是使自己嵌套。

+0

向我们展示你的mysql表的样子,并且使用的摆脱了于数据库的数组中的代码? – Arlind 2015-04-01 10:11:05

+0

@Arlind补充说。 :) – hahahaha 2015-04-01 10:18:31

+0

我添加了一个答案:) – Arlind 2015-04-01 11:57:38

回答

1

只需更改您的代码代码如下:

 $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 
      ); 
+1

BRILLIANT!谢谢! :D – hahahaha 2015-04-02 02:50:29

+0

我很高兴我帮助你:) :) – Arlind 2015-04-02 17:49:18

+0

这对于多级阵列如何工作? – 2015-06-30 20:13:38