2

I have this array I am posting in Laravel from my view to my controller, and I am trying to check if there is any values inside the array. The array is initialized and sent to the view and inside the view I have a table with inputs to fill, if the user doesn't fill the table and submits the form, the array will come back as following:

Array
(
[51] => Array
    (
        [5] => 
        [2] => 
        [8] => 
    )

[78] => Array
    (
        [18] => 
        [23] => 
        [21] => 
    )
)

for clarification and communication:

array(
   [key1]=>array
           (
              [key1_1]=>value
           )
)

and I want to check if all of value are empty or not which they are in this example, it would be something similar to empty($array) for 1 dimensional arrays.

I have tried array_filter() but it doesn't serve if the value is inside a key inside a key inside an array.

I know I can use foreach to enter to key1 and then foreach again to enter key1_1 and recursively check if the value is null or not and return false and break the loop whenever a value is not null.

But is there any other way or a method in PHP that allows checking those values? something similar to empty($array) but goes inside the array and checks value only? or something that has the logic of empty(array_filter(array_filter($array)))?

or there is no other way except recursively check each value manually by using foreach?

NOTE: I am using Laravel 5.5, PHP 7.1.9

NOTE: I am not trying if find a specific value is null, I am asking if there is a built-in method in PHP or a simpler method than the one I use to check if the values are all null or not.

3
  • You can try array_sum, if == 0 - empty Commented May 30, 2018 at 14:50
  • Is it explicitly always a two dimensional array, or could it be more or less nested than that? Commented May 30, 2018 at 15:09
  • @deceze it is explicitly always two dimensional array as in the example, it will never be more or less nested than that Commented May 30, 2018 at 15:25

3 Answers 3

5

array_map('array_filter', $array) will remove all inner empty values, bringing the array to something like:

[
    51 => [],
    78 => [
        18 => 'not empty'
    ]
]

Then array_filter that again to remove all empty arrays. In summary:

if (!array_filter(array_map('array_filter', $array))) {
    echo 'The array is completely empty';
}
Sign up to request clarification or add additional context in comments.

Comments

1

One way to do this could be to use array_reduce and pass an empty array as the start value. Then in the callback function use array_merge.

At the end you could use array_filter to remove the empty entries and then count the items in the final array.

$arrays = [
    51 => [
        5 => "",
        2 => "",
        8 => "77"
    ],
    78 => [
        18 => "",
        23 => "99",
        21 => ""
    ]
];

$result = array_reduce($arrays, function($carry, $item) {
    $carry = array_merge($carry, $item);
    return $carry;
}, []);

Test with all empty values

Test with values

1 Comment

your solution works, but I am gonna mark @deceze 's answer as the correct one since it is more accurate with less lines.
-1

This should work if you are only trying to get the number of empty slots in an array.

$myArray = ["bob", "fred", "bill", "mary", "", "jane", ""];

$count = 0;
foreach($myArray as $key=>$value) {


if(!$value) {
$count++;
}


}
echo $count;

1 Comment

empty should be replaced with !$value here. Don't use empty where a boolean test will do.

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.