Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to remove empty data from an array?

var_dump($array);

array(1)  
  {    [0]=>     
    array(4) 
    { 
     [0]=> string(0) "" 
     [1]=> string(3) "car" 
     [2]=> string(4) "bike"
     [3]=> string(1) " " 
    }   
  }

I add array_filter($array); nothing removed. so how to remove "" and " " or if have more empty space in the array?

share|improve this question

5 Answers

up vote 2 down vote accepted

I think you're trying to achieve this behavior:

<?php
$foo = array(
    array(
        'foo',
        ' ',
        'bar',
        0,
        false,
    ),
);

function array_clean(array $haystack)
{
    foreach ($haystack as $key => $value) {
        if (is_array($value)) {
            $haystack[$key] = array_clean($value);
        } elseif (is_string($value)) {
            $value = trim($value);
        }

        if (!$value) {
            unset($haystack[$key]);
        }
    }

    return $haystack;
}

print_r(array_clean($foo));

The script will output:

Array
(
    [0] => Array
        (
            [0] => foo
            [2] => bar
        )

)

Right?

share|improve this answer
 
That's quite elaborate. Generic enough to allow nested arrays, but it will still leave the number 1, while removing string '0'. –  GolezTrol Nov 27 '11 at 19:47
1  
Well, the example shows a nested array. –  alessandro1997 Nov 28 '11 at 16:46
 
You're right about that. :) –  GolezTrol Nov 29 '11 at 7:42
$array = array_filter($array, 'trim');

[edit]

This will indeed fail on some values like '0', and non-string values.

A more thorough function is:

$array = array_filter($array, function($a){
    return is_string($a) && trim($a) !== "";
});

This will only return strings that fit your request.

share|improve this answer
1  
No, trim don't work for multiple spaces –  ajreal Nov 27 '11 at 18:31
1  
What do you mean? Yes it does. –  GolezTrol Nov 27 '11 at 18:32
 
My bad .. (15 chars) –  ajreal Nov 27 '11 at 18:34
1  
Not totally correct: ideone.com/OyYyk –  codaddict Nov 27 '11 at 18:39
 
@codaddict Yes, you're right, although you should always consider what your input will be and how far you want to go. Anyway, I posted an alternative that is just a little more code. –  GolezTrol Nov 27 '11 at 18:46

Create a callback function which trims whitespace from the input element and return TRUE if the trimmed element isn't empty:

$array = array_filter($array, function($x) { 
  $x = trim($x);
  return !empty($x);
});

// Example:
$array = array(1,2,"",3," ",5);

print_r($array);
Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [5] => 5
)
share|improve this answer
 
Not totally correct: ideone.com/m3lJp –  codaddict Nov 27 '11 at 18:43

According to the doc of array_filter() :

$entry = array(
             0 => 'foo',
             1 => false,
             2 => -1,
             3 => null,
             4 => ''
          );

print_r( array_filter( $entry ) ); //Array ( [0] => foo [2] => -1 )
share|improve this answer
// removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
$result = array_filter( $array, 'strlen' );
share|improve this answer

Your Answer

 
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.