Php 非法偏移量类型:Foreach通过数组循环

Php 非法偏移量类型:Foreach通过数组循环,php,laravel,Php,Laravel,我有一个循环: @foreach($books as $listing) <h4>{{ $books[$listing]['name'] }} - {{ $books[$listing]['author'] }}</h4> <p>[[ $books[$listing]['location'] }} - {{ $books[$listing]['condition'] }}.</p> @endforeach @foreach($books

我有一个循环:

@foreach($books as $listing)
  <h4>{{ $books[$listing]['name'] }} - {{ $books[$listing]['author'] }}</h4>
  <p>[[ $books[$listing]['location'] }} - {{ $books[$listing]['condition'] }}.</p>
@endforeach
@foreach($books as$listing)
{{$books[$listing]['name']}-{{$books[$listing]['author']}
[[$books[$listing]['location']}-{{$books[$listing]['condition']}

@endforeach
它会产生一个错误:
非法偏移类型(视图:然后指向.blade.php视图路径

我的数组元素基本上可以这样引用:
$books[0]['author']
。我希望能够在数组中循环,并在每次迭代中更改[0]的值


我对这一点非常陌生,希望您能提供帮助。

您出现此错误的原因是您试图使用
$listing
作为
$books
的键。相反,您应该执行以下操作:

@foreach($books as $key => $listing)
    {{{ $listing['name'] }}} or {{{ $books[$key]['name'] }}}
@endforeach

如果使用的是
foreach
,则不必再为原始数组编制索引

$books[$listing]['name']
你能行

$listing['name']

$listing
数组中没有元素的索引,而是数组本身的元素。

要解决此错误,请使用您尝试循环的原始数组,如下所示:

 @foreach($books as $key => $listing)

{{{ $books['name'] }}} or {{{ $books['name'] }}}

 @endforeach

为什么相同的值要重复两次?