php过滤空数组

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?

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?

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

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 )
// removes all NULL, FALSE and Empty Strings but leaves 0 (zero) values
$result = array_filter( $array, 'strlen' );