Having a bit of a simple moment...

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!

share|improve this question

5 Answers

up vote 2 down vote accepted

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).

share|improve this answer
Ah, magic... Thanks Ryan :) – christian.thomas Apr 21 '11 at 22:35

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

$array = array_filter(array_map('array_filter', $array));
share|improve this answer
1  
No, this is magic! – Kalle H. Väravas Feb 18 '12 at 4:41
3  
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. – Matt K Mar 13 '12 at 14:55

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.

share|improve this answer
nice, i've never used preg_grep. i don't think this removes indices 28 and 29 from the array, though? – Ryan Apr 21 '11 at 22:43
@Ryan: True, somehow I forgot about this ;) But you already provided a good answer for how to do it... – Felix Kling Apr 21 '11 at 22:44
//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
share|improve this answer
This doesn't really address the question, which is dealing with a nested array (an array of arrays). – femtoRgon Oct 10 '12 at 21:21

You probably need to edit the code below a little.

foreach ($arr as $key => $value) {
  if($value == "") {
     unset($value[$key]);
  }
}
share|improve this answer
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 – christian.thomas Apr 21 '11 at 22:31
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 – Ryan Apr 21 '11 at 22:32
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. – Pim Reijersen Apr 21 '11 at 22:41

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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