仅删除数组中的空格,而不删除0(零)号

时间:2019-07-03 02:35:51

标签: php arrays

因此,我尝试删除多维数组中的空格。但是整数0包含在删除中。

我已经尝试过使用array_filter和array_map将其删除。

$a="array(
      [0] => test
      [1] => 0
      [2] => test
      [3] => 
      [4] => 
      [5] => test
)
array(
      [0] => test
      [1] => 
      [2] => 
      [3] => 
      [4] => 0
      [5] => test
)"
$b=array_filter(array_map('trim', $a));
print_r($b);

输出为

"array(
      [0] => test
      [2] => test
      [5] => test
)
array(
      [0] => test
      [5] => test
)"

但是预期的输出应该是这样

"array(
      [0] => test
      [1] => 0
      [2] => test
      [5] => test
)
array(
      [0] => test
      [4] => 0
      [5] => test
)"

1 个答案:

答案 0 :(得分:1)

您可以借助array_filter()strlen

$result = [];
foreach($a as $k=>$v){
    // strlen will remove all NULL, FALSE and empty strings but leaves 0 values
    $result[$k] =  array_filter( $v, 'strlen' );
}
print_r($result);

工作演示: https://3v4l.org/chq3D