简体   繁体   中英

PHP check if “multidimensional array(array([key]=>value))” values are empty

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.

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

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';
}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM