我正在尝试foreach
我的阵列但没有工作
这是我的控制器
$start = (new DateTime("2016-11-01"))->modify('first day of this month');
$end = (new DateTime("2017-02-01"))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$result = array();
foreach ($period as $dt) {
$data_month = array(
'month' => $dt->format("n"),
'year' => $dt->format("Y")
);
array_push($result, $data_month);
}
这是我的刀片
@foreach($result as $row => $innerArray)
@foreach($innerArray as $innerRow => $value)
<td>{{$value->month}}</td>
<td>{{$value->year}</td>
@endforeach
@endforeach
结果:
尝试获取非对象的属性
但如果我尝试这样,只是数组上的第一个值('月')循环
@foreach($result as $row => $innerArray)
@foreach($innerArray as $innerRow => $value)
<td>{{$value}}</td>
@endforeach
@endforeach
结果:
仅循环月份
答案 0 :(得分:2)
试试这个:
@foreach($result as $value)
<td>{{ $value['month'] }}</td>
<td>{{ $value['year'] }</td>
@endforeach
因为数组结构如下:
Array
(
[0] => Array
(
[month] => 11
[year] => 2016
)
[1] => Array
(
[month] => 12
[year] => 2016
)
[2] => Array
(
[month] => 1
[year] => 2017
)
[3] => Array
(
[month] => 2
[year] => 2017
)
)
答案 1 :(得分:1)
控制器逻辑
$start = (new DateTime("2016-11-01"))->modify('first day of this month');
$end = (new DateTime("2017-02-01"))->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 month');
$period = new DatePeriod($start, $interval, $end);
$result = array();
foreach ($period as $dt) {
$result[] = array(
'month' => $dt->format("n"),
'year' => $dt->format("Y")
);
}
刀片文件
@foreach($result as $row => $data)
<td>{{$data['month']}}</td>
<td>{{$data['year']}}</td>
@endforeach
答案 2 :(得分:0)
试试这个
@foreach($result as $result1)
{{ $result1['month'] }}
@endforeach