DD 在嵌套的 foreach 循环中不起作用

DD does not work in the nested foreach loop

我正在尝试查看变量中的内容。它是一个多重嵌套的foreach。直到 foreach 中的某个点,它才能够输出 dd。然后突然它不再起作用了,好像 Laravel 正在阅读它。

我试过 var_dump 也没有用。如果我输入随机单词,它确实会抛出错误,所以代码确实会出现。

代码:

foreach ($houses as $house) {
        foreach ($house['forms'] as $form) {
            echo 'Hello World';
            foreach ($projectForms['forms'] as $addedForm) {
                dd($form); //This is the dump I would like to see
                if($addedForm['id'] === $form['id']) {
                    array_push($addedForm['streets'], $house['fullStreet']);
                } else {
                    array_push($projectForms['forms'], array('id' => $form['id'], 'name' => $form['name'], 'percentage' => $form['percentage'], 'streets' => $house['fullStreet']));

                }
            }
        }
    }

添加$房子:

array:3 [▼
  0 => array:3 [▼
    "id" => 97641
    "fullStreet" => "SomeStreet 1"
    "forms" => array:1 [▼
      0 => array:3 [▼
        "id" => 2456
        "name" => "Some Name"
        "percentage" => 6.6666666666667
      ]
    ]
  ]
  1 => array:3 [▼
    "id" => 97642
    "fullStreet" => "SomeStreet 2"
    "forms" => array:1 [▼
      0 => array:3 [▼
        "id" => 2456
        "name" => "Some Name"
        "percentage" => 6.6666666666667
      ]
    ]
  ]
  2 => array:3 [▼
    "id" => 97643
    "fullStreet" => "SomeStreet 3"
    "forms" => array:1 [▼
      0 => array:3 [▼
        "id" => 2456
        "name" => "Some Name"
        "percentage" => 6.6666666666667
      ]
    ]
  ]
]

添加$表格:

array:3 [▼
    "id" => 2456
    "name" => "Some Name"
    "percentage" => 6.6666666666667
]

添加 $projectForms:

array:2 [▼
  "projectName" => "Some Project Name"
  "forms" => []
]

我想知道为什么 dd 在 $projectForms 的 foreach 中不起作用。在它上面的回声下,它确实有效。

您应该检查数组中是否存在第一个数据,例如:!empty()

foreach ($houses as $house) {
    if(!empty($house['forms']){
        foreach ($house['forms'] as $form) {
            if(!empty($projectForms['forms']){
                foreach ($projectForms['forms'] as $addedForm) {
                    dd($form); //This is the dump I would like to see
                    if($addedForm['id'] === $form['id']) {
                        array_push($addedForm['streets'], $house['fullStreet']);
                    } else {
                        array_push($projectForms['forms'], array('id' => $form['id'], 'name' => $form['name'], 'percentage' => $form['percentage'], 'streets' => $house['fullStreet']));

                    }
                }
            }
        }
    }
}