多维数组Laravel

时间:2018-01-12 17:38:25

标签: laravel laravel-5.1 blade laravel-blade

我是laravel的新手,我试图在刀片模板的视图中遍历这个多维数组但无法获取元素。我尝试使用嵌套的foreach,但这不起作用。我不确定还能做什么。请帮忙

Array
(
[2] => Illuminate\Support\Collection Object
    (
        [items:protected] => Array
            (
            )

    )

[3] => Illuminate\Support\Collection Object
    (
        [items:protected] => Array
            (
                [0] => stdClass Object
                    (
                        [ActivityID] => 1
                        [WaiverID] => 3
                        [ProgramID] => 1
                        [ActivityName] => Waiver3 Activity 1
                        [WaiverName] => Waiver 3
                        [ID] => 3
                        [WaiverDescription] => 
                        [ProgramID] => 1
                    )

                [1] => stdClass Object
                    (
                        [ActivityID] => 2
                        [WaiverID] => 3
                        [ProgramID] => 1
                        [ActivityName] => Waiver3 Activity 2
                        [WaiverName] => Waiver 3
                        [ID] => 3
                        [WaiverDescription] => 
                        [ProgramID] => 1
                    )

                [2] => stdClass Object
                    (
                        [ActivityID] => 3
                        [WaiverID] => 3
                        [ProgramID] => 1
                        [ActivityName] => Waiver3 Activity 3
                        [WaiverName] => Waiver 3
                        [ID] => 3
                        [WaiverDescription] => 
                        [ProgramID] => 1
                    )

            )

    )

当我做这样的foreach时

@foreach ($waiverActivityforTable as$waiverActivityforTableValue)           
    {{ $waiverActivityforTableValue }}
@endforeach

我明白了:

[] [] [{"ActivityID":1,"WaiverID":3,"PHAProgramID":1,"ActivityName":"Waiver3 
Activity 1","HCVWaiverName":"Waiver 
3","ID":3,"HCVWaiverDescription":null,"phaProgramID":1},
{"ActivityID":2,"WaiverID":3,"PHAProgramID":1,"ActivityName":"Waiver3 
Activity 2","HCVWaiverName":"Waiver 
3","ID":3,"HCVWaiverDescription":null,"phaProgramID":1},
{"ActivityID":3,"WaiverID":3,"PHAProgramID":1,"ActivityName":"Waiver3 
Activity 3","HCVWaiverName":"Waiver 
3","ID":3,"HCVWaiverDescription":null,"phaProgramID":1}] 

累了嵌套的foreach: 我试过了:

    @foreach ($waiverActivityforTable as $waiverActivityforTableValue)
        @foreach ($waiverActivityforTableValue as $test)
            {{ $test }}
        @endforeach
    @endforeach

但是我收到一个错误:htmlspecialchars()期望参数1是字符串,给定对象

2 个答案:

答案 0 :(得分:0)

似乎嵌套的多维数组可能为空,在回显之前检查它

@if(is_array($waiverActivityforTable)
     @foreach ($waiverActivityforTable as $waiverActivityforTableValue)
      @endif
          @if(is_array($waiverActivityforTableValue)
            @foreach ($waiverActivityforTableValue as $test)
             @endif
              @isset($test)
                {{ $test }}
                 @endisset
            @endforeach
        @endforeach

根据您的数据,对is_arrayisset的具体检查可能会有所不同。

话虽这么说,你试图解析的多维数组应该以更好的方式初始化。

答案 1 :(得分:0)

使用Collection::toArray方法将集合转换为普通的PHP数组:

 @foreach ($waiverActivityforTable->toArray() as $waiverActivityforTableValue)
                                  ^^^^^^^^^^^
    @foreach ($waiverActivityforTableValue as $test)
        {{ $test }}
    @endforeach
@endforeach