Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

Current array:

Array (
    [name] => Array (
        [name1] => Array (
            [0] => some value1
        )
        [name2] => Array (
            [0] => some value2
        )
        [name3] => Array (
            [0] =>
        )
) 

Wanted array:

Array (
    [name] => Array (
        [name1] => Array (
            [0] => some value1
        )
        [name2] => Array (
            [0] => some value2
        )
) 

Since name3[0] doesn't contain any value, it needs to be removed. From what I read, I should use array_filter for this, but I can't get it to work.

share|improve this question

marked as duplicate by Daniel Vérité, Yotam Omer, Marc Audet, Mike Fielden, fedorqui Jul 26 '13 at 15:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Why is your data structure like that? Why not a simple $names = [['name' => 'Jon'], ...] –  elclanrs Jul 25 '13 at 21:16

3 Answers 3

up vote 3 down vote accepted

Recursive function, it will remove all empty values and empty arrays from input array:

//clean all empty values from array
function cleanArray($array)
{
    if (is_array($array))
    {
        foreach ($array as $key => $sub_array)
        {
            $result = cleanArray($sub_array);
            if ($result === false)
            {
                unset($array[$key]);
            }
            else
            {
                $array[$key] = $result;
            }
        }
    }

    if (empty($array))
    {
        return false;
    }

    return $array;
}

I have tested it on this example, it works no matter how deep is array:

$array = array(
    'name' => array(
        'name1' => array(0 => 1),
        'name2' => array(0 => 3, 1 => array(5 => 0, 1 => 5)),
        'name3' => array(0 => '')
    )
);

Hope this helps :)

share|improve this answer

You need to feed array_filter a predicate (function) that determines if the [0] sub-element of each array element is empty or not. So:

$array = array_filter($array, function($item) { return !empty($item[0]); });

Be aware that empty is not very picky: it will result in removing any item whose [0] sub-element is the empty string, false, null, 0 or "0" -- it will also remove items that do not have a [0] sub-element at all. If you need something more surgically targeted the test needs to be fine-tuned.

share|improve this answer
1  
$array in your example needs to be $array['name'] if using the OP's original array –  AlexP Jul 25 '13 at 21:29
    
@AlexP: Definitely. I 'm not sure that warrants an edit though. –  Jon Jul 25 '13 at 21:32
    
Ok, +1 anyway :-) –  AlexP Jul 25 '13 at 21:42

Could be accomplished using a recursive function:

$arr = array('test', array('',0,'test'), array('',''));

print_r(clean_array($arr));

function clean_array($array, $isRepeat = false) {
    foreach ($array as $key => $value) {
        if ($value === null || $value === '') { unset($array[$key]); }
        else if (is_array($value)) {
            if (empty($value)) { unset($array[$key]); }
            else $array[$key] = clean_array($value);
        }
    }
    if (!$isRepeat) {
        $array = clean_array($array,true);
    }
    return $array;
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.