Laravel 不读取 blade 语法中的数组索引

Laravel not reading array index in blade syntax

我有一个多维数组,我将其传递给 laravel 中的视图。该数组用于使用 blade 语法将数据输出到视图。但是当我尝试访问 @foreach 循环中的键时,我收到错误 Invalid argument supplied for foreach() .
当我在 @foreach () 循环之前的行中输出 php 标记中的相同数组索引时,没有错误。 有人可以告诉我为什么会发生这种情况吗?

数组 var_dumped : $item['all']

@php
    echo var_dump($item['all']);
    die();
@endphp
array (size=1)
  0 => 
    array (size=22)
      'withdraw_prod' => int 0
      'class_id' => int 10
      'optional' => int 0
      'extra' => int 0
      'stage' => string 'NA' (length=2)
      'pest' => string 'NA' (length=2)
      'product' => string 'Monitering' (length=10)
      'description' => null
      'active' => null
      'class' => string 'Ander' (length=5)
      'rac_code' => null
      'htwoo' => string '0' (length=1)
      'prod_ha' => string '2.98' (length=4)
      'kgl_ha' => string '0.0000' (length=6)
      'hundred_met' => null
      'total' => string '0.0000' (length=6)
      'cost_kgl' => null
      'cost_total' => null
      'heading' => null
      'header' => int 0
      'hex' => null
      'is_na' => boolean true

下一行出现错误的代码:

@foreach ($item['all'] as $k => $spray)

一些日志输出:

<?php $__currentLoopData = $item['all']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $k => $spray): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>

Laravel 5.6.39

编辑: 我正在尝试将数据输出到从控制器生成 pdf 的视图:

$pdf = PDF::loadView('direct.tory.here.is.pdf', compact('program', 'taboo', 'colour', 'footnotes'))->setPaper('a4', 'landscape');

视图中的循环是:

@foreach ($program as $key => $item)
    @if(!in_array($key, $taboo))
         @php
             $cnt = 0;
             $extra_row = 0;
             //echo var_dump($item['all']);
         @endphp
         @foreach ($item['all'] as $k => $spray)
              @php
                  // for in case array dimensions differ
                  $sprays = (array_key_exists(0, $spray)) ? $spray[0] : $spray;
                      if(!empty($sprays['description'])) {
                         $extra_row++;
                       }
                @endphp
          @endforeach
    @endif
    @php
         $col++;
    @endphp
@endforeach

解决了 这有点尴尬,但我创建了一个带有禁忌键的数组,它从我的主数组 $program 中过滤掉了非数组值。为了两个月前的测试,添加了一个包含整数的额外虚拟键 => 值用于测试,但从未添加到禁忌列表中。这个虚拟整数值正在滑过,导致 @foreach 循环无法迭代 $item['all'],该数组索引不存在。 感谢所有评论者的意见。

我创建了一个带有禁忌键的数组,它从我的主数组 $program 中过滤掉了非数组值。为了两个月前的测试,添加了一个包含整数的额外虚拟 key => value 用于测试,但从未添加到禁忌列表中。这个虚拟整数值正在滑过,导致 @foreach 循环无法遍历 $item['all'],该数组索引不存在。

所以解决方案是疏忽而不是 type checking 在使用值之前。