简体   繁体   中英

associative array check any column is empty or not

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.

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

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

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