0

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

1
  • 1
    Could you also add the expected result? What should your code produce, if it worked? And what does it produce now? Commented Oct 26, 2017 at 14:54

2 Answers 2

1
// 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

Sign up to request clarification or add additional context in comments.

Comments

0

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.