Parker P.
Parker P.

Reputation: 9

PHP Compare Arrays and clear all key if one in a array is empty

I'm looking for a solution to compare multiplay arrays with each of them i want to unset in all arrays if one key is empty. So as a example if [keywords] is empty i want to unset in all arrays [keywords]. Here are my arrays that i print_r.

Array
(
    [0] => Array
        (
            [id] => 1
            [pid] => 3
            [sorting] => 128
            [tstamp] => 1503039725
            [title] => test
            [alias] => test-3
            [author] => 1
            [inColumn] => main
            [keywords] => 
            [showTeaser] => 
            [teaserCssID] => 
            [teaser] => 
            [printable] => 
            [customTpl] => 
            [protected] => 
            [groups] => 
            [guests] => 
            [cssID] => 
            [space] => 
            [published] => 1
            [start] => 
            [stop] => 
        )

    [1] => Array
        (
            [id] => 2
            [pid] => 3
            [sorting] => 256
            [tstamp] => 1503045056
            [title] => test 2
            [alias] => test-2
            [author] => 1
            [inColumn] => main
            [keywords] => 
            [showTeaser] => 
            [teaserCssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
            [teaser] => 
            [printable] => 
            [customTpl] => 
            [protected] => 
            [groups] => 
            [guests] => 
            [cssID] => a:2:{i:0;s:0:"";i:1;s:0:"";}
            [space] => a:2:{i:0;s:0:"";i:1;s:0:"";}
            [published] => 1
            [start] => 
            [stop] => 
        )

)

What i have tried so far is

print_r($arrResult);

    foreach($arrResult as $Result)
        {   
            foreach ($Result as $arrKey => $arrField)
            {
                if(!empty($arrField)) 
                {
                    $arrAllowedField[$arrKey] = $arrKey;
                }
            }
        }

That build a array that contains the key which has values. But the problem is, that it also add empty fields of a other array.

Thanks

Upvotes: 0

Views: 38

Answers (2)

splash58
splash58

Reputation: 26153

// remove empty entries in each array
$ar = array_map('array_filter', $ar);
// find keys having not empty value at least in one array
$temp = array_intersect_key(...$ar);
// save only keys from temp array 
foreach($ar as &$item) {
  $item = array_intersect_key($item, $temp);
}

demo

Upvotes: 1

Thamaraiselvam
Thamaraiselvam

Reputation: 7080

It removes the all the empty values from the array

$arrResult = array_map('array_filter', $arrResult);
$arrResult = array_filter( $arrResult );

echo "<pre>";
print_r($arrResult);

Output

Array
(
    [0] => Array
        (
            [id] => 1
            [pid] => 3
            [sorting] => 128
            [tstamp] => 1503039725
            [title] => test
            [alias] => test-3
            [author] => 1
            [inColumn] => main
            [published] => 1
        )

)

Upvotes: 0

Related Questions