SandyK
SandyK

Reputation: 465

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.

Upvotes: 1

Views: 61

Answers (1)

Shivrudra
Shivrudra

Reputation: 684

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
)

Upvotes: 3

Related Questions