在Laravel Blade模板中循环嵌套数组

时间:2016-03-15 16:08:46

标签: php laravel blade laravel-5.2 laravel-blade

我有以下代码:

@foreach($data['daily'] as $date => $dailyData)
    <tr>
        <td>{{$date}}</td>
        @foreach($dailyData as $key => $value)
            <td>
                <span>{{$value}}</span>
                <strong>{{$data['another_index'][$date][$key]}}</strong>
            </td>
        @endforeach
    </tr>
@endforeach

这给我一个错误

[ErrorException]
Undefined index: date

执行代码时。实际上我在another_index有值,我可以打印它。

提前感谢您的帮助。

[another_index] => Array
        (
            [2016-03-15] => Array
                (
                    [key] => 100.00%
                    [key1] => 0.00%
                    [key2] => 0.00%
                )

            [2016-03-14] => Array
                (
                    [key] => 10.00%
                    [key1] => 20.00%
                    [key2] => 30.00%
                )

1 个答案:

答案 0 :(得分:0)

查看您的示例数组,这应该会为您提供您尝试实现的目标:

@foreach($data as $date => $dailyData)
    <tr>
        <td>{{$date}}</td>
        @foreach($dailyData as $key => $value)
            <td>
                <span>{{$key}}</span>
                <strong>{{$value}}</strong>
            </td>
        @endforeach
    </tr>
@endforeach