Foreach 数组上的数组 php

Foreach array on array php

我正在尝试 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);
    }

这是我的 blade

@foreach($result as $row => $innerArray)
   @foreach($innerArray as $innerRow => $value)
   <td>{{$value->month}}</td>
   <td>{{$value->year}</td>
   @endforeach
@endforeach

结果:

Trying to get property of non-object

但如果我这样尝试,只有数组中的第一个值 ('month') 正在循环

@foreach($result as $row => $innerArray)
   @foreach($innerArray as $innerRow => $value)
   <td>{{$value}}</td>
   @endforeach
@endforeach

结果:

looping month only

试试这个:

@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
        )

)

试试这个

@foreach($result as $result1)
  {{ $result1['month'] }}
@endforeach

控制器逻辑

    $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")
        );
    }

Blade 文件

@foreach($result as $row => $data) 
    <td>{{$data['month']}}</td>
    <td>{{$data['year']}}</td>
@endforeach