简体   繁体   English

PHP检查“多维数组(array([key] => value))”的值是否为空

[英]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. 我有这个数组,我正在从Laravel中将其发布到我的控制器,并且试图检查数组中是否有任何值。 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. 并且我想检查所有value是否为空,在此示例中,它们是否与一维数组的empty($array)类似。

I have tried array_filter() but it doesn't serve if the value is inside a key inside a key inside an array. 我已经试过array_filter()但是如果值在数组内的键内的键内,则它不起作用。

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. 我知道我可以使用foreach进入到key1 ,然后foreach再次进入key1_1和递归检查值为null或不并返回false并打破循环一旦某个值不为空。

But is there any other way or a method in PHP that allows checking those values? 但是,PHP中是否还有其他方法或方法可以检查这些值? something similar to empty($array) but goes inside the array and checks value only? 类似于empty($array)但在数组内部并仅检查value or something that has the logic of empty(array_filter(array_filter($array))) ? 或具有empty(array_filter(array_filter($array)))逻辑的东西?

or there is no other way except recursively check each value manually by using foreach ? 还是没有其他方法,只能通过使用foreach手动递归检查每个值?

NOTE: I am using Laravel 5.5, PHP 7.1.9 注意:我使用的是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. 注意:我不是在尝试查找特定值是否为null,而是询问PHP中是否有内置方法或比用于检查值是否全部为null的方法简单的方法。

One way to do this could be to use array_reduce and pass an empty array as the start value. 一种实现方法是使用array_reduce并将空数组作为起始值。 Then in the callback function use array_merge . 然后在回调函数中使用array_merge

At the end you could use array_filter to remove the empty entries and then count the items in the final array. 最后,您可以使用array_filter删除空条目,然后计算最终数组中的项目。

$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: array_map('array_filter', $array)将删除所有内部的空值,从而使数组变为类似:

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

Then array_filter that again to remove all empty arrays. 然后再次使用array_filter删除所有空数组。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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