如何在laravel中检查多维数组是否为空

时间:2016-04-26 21:25:58

标签: laravel

我正在尝试检查嵌套数组中是否有空数组。

这是我从表格中得到的。

array:15 [▼
  "_token" => "h4aR4xJlWhZveRKbAgHzgzHWSKSqyhVKb7OHAgWH"
  "name" => "Test office"
  "is_department" => "0"
  "hours" => "1-3"
  "description" => "Description"
  "content" => "<p>Content</p>"
  "street" => "123 Street"
  "city" => "Foomania"
  "state" => "Sweet state"
  "postal" => "98234"
  "phone" => "5748293212"
  "fax" => "2123131233"
  "email" => "test@domain.tld"
  "additional-page" => ""
  "office_fees" => array:4 [▼
    0 => array:2 [▼
      "description" => ""
      "fee" => ""
    ]
    1 => array:2 [▼
      "description" => ""
      "fee" => ""
    ]
    2 => array:2 [▼
      "description" => ""
      "fee" => ""
    ]
    3 => array:2 [▼
      "description" => ""
      "fee" => ""
    ]
  ]
]

如何检查office_fees中是否有空数组?

为了清楚起见,office_fees将始终返回至少一个数组。我想要的是能够确定office_fees是否需要保存到另一个模型中。

2 个答案:

答案 0 :(得分:1)

不确定您要找的是什么,但是:

empty($data['office_fees'])

检查数组是否设置而不是空。如果你想检查一个空数组,试试这个:

if (is_array($data['office_fees']) && !empty($data['office_fees']))

答案 1 :(得分:0)

在laravel 5.2中,您可以验证数组

$validator = Validator::make($request->all(), [
   'office_fees.*.description' => 'required',
]);

来源:Laravel documentation

相关问题