在php中从数组中删除包含空格和新行的元素
我面临着从数组元素中删除空白和新行的问题 我试过下面的代码在php中从数组中删除包含空格和新行的元素,php,arrays,trim,Php,Arrays,Trim,我面临着从数组元素中删除空白和新行的问题 我试过下面的代码 $explode_string = explode("\n", $GetGroupListOutput); $new_array = array(); foreach ($explode_string as $value) { $value1 = preg_replace('/\s+/', ' ', trim($value)); if(!empty($value1)
$explode_string = explode("\n", $GetGroupListOutput);
$new_array = array();
foreach ($explode_string as $value) {
$value1 = preg_replace('/\s+/', ' ', trim($value));
if(!empty($value1))
{
$new_array[] = $value1;
}
}
var_dump($new_array);
输出是这样的
array(107147) {
[0]=>
string(10) ""
[1]=>
string(8) "
"
[2]=>
string(40) "Import Data"
[3]=>
string(9) "
"
[4]=>
string(6) ""
[5]=>
string(12) ""
[6]=>
string(13) ""
[7]=>
string(36) "All Masters"
我的api输出如下所示
Import Data
All Masters
KRISHNA INTERIORS GSTR 3B
-1
上面一行显示了我的api的输出。并且输出格式不正确。匹配“空白和新行”的最简单操作是
trim()
,这使其成为一种简单的筛选操作:
$input=[
"",
"
",
“导入数据”,
"
",
" ",
" ",
" ",
“所有大师”,
];
$output=array_filter($input,'trim');
变量转储(输出);
//数组(2){[2]=>string(11)“导入数据”[7]=>string(11)“所有主机”}
匹配“空白和新行”的最简单操作是trim()
,这使其成为一种简单的筛选操作:
$input=[
"",
"
",
“导入数据”,
"
",
" ",
" ",
" ",
“所有大师”,
];
$output=array_filter($input,'trim');
变量转储(输出);
//数组(2){[2]=>string(11)“导入数据”[7]=>string(11)“所有主机”}
告诉我你传递的字符串是什么?@Siddharthasesunuri字符串来自其他Webhook,这是不正确的。这就是为什么我要尝试这个。你的输入是什么?试着使用如果($value1!='')
而不是空的
()我不明白为什么字符串(10)是为空值而来的???告诉我你传递的字符串是什么?@Siddharthasesunuri字符串来自其他Webhook,它不正确。这就是我尝试此操作的原因。你的输入是什么?尝试使用if($value1!='')
而不是empty
()我不明白为什么字符串(10)是为空值而来的???