10

How do I loop through this array and remove any empty values:

[28] => Array
    (
        [Ivory] => 
        [White] => 
    )

[29] => Array
    (
        [Ivory] => 
        [White] => 
    )

[30] => Array
    (
        [Ivory] => 
        [White] => 36
    )

[31] => Array
    (
        [White] => 24
    )

So say it'd remove 28, 29 and just Ivory from 30...

Thanks!

6 Answers 6

39

I see you already have a working solution, but just for fun, with array_map goodness:

$array = array_filter(array_map('array_filter', $array));
Sign up to request clarification or add additional context in comments.

2 Comments

black magic indeed! I should point out though that this will only work for nested arrays. reg arrays only need array_filter() without the callback.
This works because "If no callback is supplied (to array_filter), all entries of array equal to FALSE will be removed" (from the manual). Also, what @MattK said.
4

I believe this will do what you're looking for:

foreach( $array as $key => $value ) {
    if( is_array( $value ) ) {
        foreach( $value as $key2 => $value2 ) {
            if( empty( $value2 ) ) 
                unset( $array[ $key ][ $key2 ] );
        }
    }
    if( empty( $array[ $key ] ) )
        unset( $array[ $key ] );
}

It will loop through your outer array, descend into any arrays it contains and remove keys whose values are empty. Then, once it's done that, it will remove any keys from the outer array whose subvalues were all empty, too.

Note that it wouldn't work for a generic array, just the one you've provided (two-dimensional).

Comments

1

Another way, using preg_grep:

foreach($array as $key => $subarray) {
    $array[$key] = preg_grep('/^$/', $subarray, PREG_GREP_INVERT);
}

Update: Forgot about removing the empty arrays. The technique shown in @Ryan's answer can also be applied here.

2 Comments

nice, i've never used preg_grep. i don't think this removes indices 28 and 29 from the array, though?
@Ryan: True, somehow I forgot about this ;) But you already provided a good answer for how to do it...
0

For anyone who is looking for cleaning a variable which is a simple array or a multidimensional array (nested array with unknown depth) here is my proposed solution: (it is created as a static function in a class but can work also outside of a class -> just remove self::)

    public static function clean($Array, $Strict=true)
    {
        if( ! is_array($Array))
        {
            return $Array;
        }
        $cleaned = array ();

        foreach ($Array as $key => $value)
        {
            if($Strict)
            {
                if(!empty($value))
                {
                    $tmp = self::clean($value,$Strict);
                    if(!empty($tmp))
                    {
                        $cleaned[$key] = $tmp;
                    }
                }
            }//strict
            else
            {
                if(is_array($value))
                {
                    if(!empty($value))
                    {
                        $tmp = self::clean($value,$Strict);
                        if(!empty($tmp))
                        {
                            $cleaned[$key] = $tmp;
                        }
                    }
                }
                elseif(strlen($value) > 0)
                {
                    $tmp = self::clean($value,$Strict);
                    if(strlen($tmp) > 0)
                    {
                        $cleaned[$key] = $tmp;
                    }
                }
            }//not strict
        } //end foreach

        return $cleaned;
    }

Hope save someone time :) Cheers

Comments

-1

You probably need to edit the code below a little.

foreach ($arr as $key => $value) {
  if($value == "") {
     unset($value[$key]);
  }
}

3 Comments

Hmm, doesn't seem to be working... I put a die; in the if statement, but it doesn't seem to die - I.E. it's not being matched
this won't work, because $value is going to be an array; he wants to remove empty keys whose values are empty from both the inner and outer array
Yea, it was a short code example to show looping, checking and unsetting. I was working on functional code but you posted the solution before I was done.
-1
//Check Array Remove Empty Key
$ixx=0; $ix=0;//Set Array First.
while(end(array_keys($a1))>=$ix){ //Check Last Key in Array
if($a1[$ix]!=""){ //Check Empty Key
    $ax[$ixx]=$a1[$ix];$ixx++; } //Remove Empty Key
$ix++;
}
//End Check Array Remove Empty Key

print_r($ax);//Print Array After remove Empty Key

1 Comment

This doesn't really address the question, which is dealing with a nested array (an array of arrays).

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.