首页 > 解决方案 > 将多个数组合并为一个后接收错误

问题描述

我将一组数组分组在一个数组下$Final_updts[],如下所示: 请注意,这些数组在组合之前工作正常。

array(5) { 
[0]=> array(0) { } 
[1]=> array(0) { } 
[2]=> array(3) { 
  [0]=> object(stdClass)#372 (21) { 
  ["id"]=> int(8) ["created_at"]=> string(19) "2019-11-08 11:07:55" 
  ["updated_at"]=> string(19) "2019-11-08 11:07:55" 
  ["title"]=> string(42) "question 1" ["body"]=> string(47) "Answer 1"}
}

然后我将它返回到视图:

return view('updates', ['f_updt' => $Final_updts]);

看法 :

@if(!empty($f_updt))
@foreach($f_updt as $f_updts)
<div class="post00" >
  <p>Other posts</p>
  <a href="/sharp/posts/{{ $f_updts->id }}"> 
    <h3> {{$f_updts->title}}  </h3>
    <br>
    <h5> {{$f_updts->body}}  </h5>
  </a>
</div>
<br>
@endforeach
@endif

我收到此错误:

Trying to get property of non-object

标签: phparrayslaravelobject

解决方案


@foreach($f_updt as $x => $f_updts)

   @foreach($f_updts as $f_updts1)
      @if(isset($f_updts1))
        @if(isset($f_updts1->id))

            <div class="post00">
                <p>Other posts</p>
                <a href="/sharp/posts/{{ $f_updts1->id }}">
                    <h3> {{$x}}  </h3>
                    <br>
                    <h4> {{$f_updts1->title}}  </h4>
                    <br>
                    <h5> {{$f_updts1->body}}  </h5>
                </a>
            </div>
            <br>
          @endif
        @endif
    @endforeach
@endforeach

推荐阅读