如何在 laravel 中将数组转换为字符串
How to convert array to string in laravel
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
这return类似于数组格式
[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]
我想隔离 DATE
和 point_no
。例如
$year=['2015','2016','2017','2018','2019','2020'];
我试过 $date['DOCDATE']
来隔离它不起作用的日期。有没有更好的方法来解决这个问题??
试试这个查询 -
$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
return [Carbon::parse($q->DATE)->format('Y')];
});
您可以使用array_map函数。
$extractYear = function($date) {
return substr($date["DATE"], 0, 4);
};
$years = array_map($extractYear, $date);
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
$date_array = array();
foreach($date as $dat){
array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}
return $date_array;
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
这return类似于数组格式
[{"DATE":"2021-06-11 00:00:00","point_no":"SRT-358"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-359"},{"DATE":"2021-06-11 00:00:00","point_no":"SRT-360"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-357"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-356"},{"DATE":"2021-06-10 00:00:00","point_no":"SRT-355"},{"DATE":"2021-06-09 00:00:00","point_no":"SRT-348"}]
我想隔离 DATE
和 point_no
。例如
$year=['2015','2016','2017','2018','2019','2020'];
我试过 $date['DOCDATE']
来隔离它不起作用的日期。有没有更好的方法来解决这个问题??
试试这个查询 -
$years= gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get()->map(function ($q) {
return [Carbon::parse($q->DATE)->format('Y')];
});
您可以使用array_map函数。
$extractYear = function($date) {
return substr($date["DATE"], 0, 4);
};
$years = array_map($extractYear, $date);
$date = gc_head::select('DATE','point_no')->orderBy('DOCDATE','desc')->limit(7)->get();
$date_array = array();
foreach($date as $dat){
array_push($date_array, \Carbon\Carbon::parse($dat["DATE"])->format('Y');
}
return $date_array;