Php 仅删除数组中的空格,而不删除0(零)号
因此,我试图删除多维数组中的空格。但删除时包含整数0 我已经尝试使用数组过滤器和数组映射来删除它Php 仅删除数组中的空格,而不删除0(零)号,php,arrays,Php,Arrays,因此,我试图删除多维数组中的空格。但删除时包含整数0 我已经尝试使用数组过滤器和数组映射来删除它 $a="array( [0] => test [1] => 0 [2] => test [3] => [4] => [5] => test ) array( [0] => test [1] => [2] => [3] =
$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
)"
您可以借助
array\u 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);
工作演示:阵列过滤器是问题所在,而不是
修剪。ohhh所以在我的代码中,我想我甚至过滤了0值?我不能实现foreach函数,但我使用了array_过滤器($v,'strlen');成功了!!!非常感谢。