1

I'm trying to check if there is an empty array in the nested arrays.

This is what I get from my form.

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" => ""
    ]
  ]
]

How can I check if there is empty array in office_fees ?

Just to be clear, office_fees will always return at least one array. What I'm trying to is to be able to determine whether the office_fees need to be saved into another model.

1
  • I have tried with array_collapse to make the multidimensional array into single array and checked my key is empty or not. I guess, this might be the solution to get what I want. Commented Apr 27, 2016 at 14:07

2 Answers 2

1

Not sure what You're looking for but:

empty($data['office_fees'])

checks if an array isset and not empty. If You want to check an empty array try this:

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

Sign up to request clarification or add additional context in comments.

1 Comment

I would like to check whether any of the 'description' or 'fee' is empty. currently, this will always return true as I will always get array within the 'office_fees'
0

In laravel 5.2 you can validate arrays

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

Source: Laravel documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.