2016-04-26 83 views
1

我試圖檢查是否有嵌套數組中的空數組。如何檢查多維數組是否爲空或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" => "[email protected]" 
    "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是否需要保存到另一個模型。

+0

我試圖與'''array_collapse'''使多維數組爲單一陣列,並檢查我的關鍵是空的。我想,這可能是獲得我想要的解決方案。 – Nomadme

回答

0

在laravel 5.2可以驗證陣列

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

來源:Laravel documentation

1

不知道你正在尋找,但:

empty($data['office_fees'])

檢查數組isset和不是空的。如果你想查看一個空數組試試這個:

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

+0

我想檢查一下'description'或'fee'是否爲空。目前,這將始終返回'''true''',因爲我總是會在'office_fees' – Nomadme