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

时间:2022-01-29 21:20:20

I have associative array like -

我有关联数组像 -

[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
       )

I need to check is there any column having all the values are blank. That means it should return only domain from above array because it is blank everywhere.

我需要检查是否有任何列的所有值都是空白的。这意味着它应该只从上面的数组返回域,因为它在任何地方都是空白的。

Is there any way to do this with minimum use of forloop? I need to check this for all these columns.

有没有办法在最少使用forloop的情况下做到这一点?我需要检查所有这些列。

1 个解决方案

#1


3  

This will work if your subarray having same number of keys.Like "Date, id, Type etc".

如果您的子阵列具有相同数量的键,这将起作用。类似于“日期,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);

Output:

Array
(
    [0] => domain
)

#1


3  

This will work if your subarray having same number of keys.Like "Date, id, Type etc".

如果您的子阵列具有相同数量的键,这将起作用。类似于“日期,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);

Output:

Array
(
    [0] => domain
)