php - 仅删除数组中的空格,但不删除 0(零)数字
问题描述
所以我试图删除多维数组中的空格。但整数 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
)"
解决方案
你可以在和的帮助下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
推荐阅读
- c - 如何在c编程中找到atof函数中的数字总和
- pine-script - tradingview pinescript 不能在安全函数中使用可变变量作为参数
- azure-devops - Azure 管道错误:找不到与模板文件模式匹配的任何文件
- typescript - 作为参数的函数
- vue.js - 事件“xxx”的无效处理程序:未定义
- java - 将值从一个类传递到主类?
- c# - gRPC(.net 核心实现)可以使用 WCF 服务吗?
- bash - 如果grep在while循环中不返回任何内容,如何仅显示输出
- python-3.x - 散景线图将每个点连接到第一个点,出了什么问题?
- wpf - 停止 wpf 动画失败并出现 InvalidCastException