关联数组检查任何列是否为空

时间:2018-07-05 09:07:39

标签: php arrays

我有类似的关联数组-

[0] => Array
       (
           [date] => 2018-06-22
           [id] => 2282991
           [type] => VIDEO
           [domain] => 
           [code] => Austin
           [address] => Phone
       )

   [1] => Array
       (
           [date] => 2018-06-22
           [id] => 2282991
           [type] => VIDEO
           [domain] => 
           [code] => 
           [address] => Phone
       )

   [3] => Array
       (
           [date] => 2018-06-22
           [id] => 2282991
           [type] => VIDEO
           [domain] => 
           [code] => Austin
           [address] => Phone
       )

我需要检查是否所有值都为空的列。 这意味着它应该只返回上面数组中的domain,因为到处都是空白。

有什么办法可以减少对forloop的使用?我需要检查所有这些列。

1 个答案:

答案 0 :(得分:3)

如果您的子数组具有相同数量的键(例如“日期,ID,类型等”),这将起作用。

 $array = [ 
        [ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"],
        [ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "", "address" => "Phone"],
        [ "date" => "2018-06-22", "id" => 2282991, "type" => "VIDEO", "domain" =>'', "code" => "Austin", "address" => "Phone"]
    ];

   $empty = [];     
    foreach($array[0] as $key=>$val){
        $error = array_column($array, $key);
        if(empty (array_filter($error)) ) {
            $empty[] =  $key;
        }
    }
print_r($empty);

输出:

Array
(
    [0] => domain
)