如何将数据提取到数组值

时间:2018-03-26 02:08:42

标签: php arrays laravel highcharts

$match = DiraChatLog::where('status','=','Match')
->selectRaw('DATE(created_at) as date, COUNT(1) as cnt') // created_at here is datetime format, only need the date portion using `DATE` function
->orderBy("date")
->groupBy("date")
->get() // return result set (collection) from query builder
->pluck('cnt') // only need the cnt column
->values() // array_values
->all(); // convert collection to array

使用上面的代码我在我的视图中得到输出,如:
enter image description here

但我必须稍微更改我的代码才能获得另一个日期值,当我使用我的代码

$matchs = DiraChatLog::where('status','=','Match')->whereBetween('date_access', [$request->from, $request->to])->get();
// $array[] = [];
foreach ($matchs as $key => $match) {
    $day = substr($match->date_access, 0, 10);
    if(isset($array[$day]))
    {
      $array[$day]++;
    } else 
    {
      $array[$day] = 1;
    }
    $match = $array;
}

现在我在我的观点中输出如下:
enter image description here

现在我如何将它们变成阵列?例如它应该是[" 2018-03-04",3]等等,具有以下日期和价值..我该怎么做?我这样做的原因是因为当我插入值以在highcharts上查看时,第一种方式工作,但是第二种方式不能查看

1 个答案:

答案 0 :(得分:0)

使用Laravel的收集方法transform来转换收集结果。然后将集合转换为数组。

这样的事情:

aaaa