[英]Remove element that contain empty space and new line from array in php
I am facing problem to removing white space and new line from array element. 我面临从数组元素中删除空白和换行的问题。
I have tried below code 我试过下面的代码
$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);
Output is like this 输出是这样的
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"
My Output from api is like below. 我的api输出如下。
Import Data
All Masters
KRISHNA INTERIORS GSTR 3B
-1
Above line show the output of my api. 上面的行显示了我的api的输出。 And output is not proper format.
并且输出格式不正确。
The simplest operation to match "white space and new line" would be trim()
, which makes it an easy filter operation: 匹配“空白和换行”的最简单的操作是
trim()
,这使得它很容易进行过滤操作:
$input = [
"",
"
",
"Import Data",
"
",
" ",
" ",
" ",
"All Masters",
];
$output = array_filter( $input, 'trim' );
var_dump( $output );
// array(2) { [2]=> string(11) "Import Data" [7]=> string(11) "All Masters" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.