我需要清理以下PHP数组并删除所有空变量。 该数组是由字符串组成的,该字符串是从第三方软件复制而来,包含许多换行符。
我尝试和失败的方式如下。
数组:
Array (
[0] =>
[1] => 1.PAUL/JOYTEE RANI MRS
[2] => 2.PAUL/SATTYAJ MSTR(CHD/05NOV04)
[3] =>
[4] => 3.PAUL/SATTYAM MSTR(CHD/23JUN08)
[5] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2
[6] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2
[7] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2
[8] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 )
我尝试过但失败的功能:
public static function filter_array_empty_value($arr){
foreach($arr as $k=>$v){
$v = str_replace("\n","",$v);
$v = str_replace(" ","",$v);
$v = trim($v);
for($i=1;$i<=50;$i++){
$v = str_replace("\n","",$v);
$v = str_replace(" ","",$v);
}
if($v == " " || $v == NULL || empty($v) || strlen($v)<1){
unset($arr[$k]);
}
}
return $arr;
}
注意: 修剪后,数组[0]的字符串长度为4。
非常感谢帮助。
答案 0 :(得分:4)
假设您的行数组是file()函数的结果,只需使用自己的文件函数支持的标志。像这样:
$filelines = file('filename.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES)
使用可选标志时FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES文件函数返回除空行之外的所有行
答案 1 :(得分:2)
$arr = array (
0 => "",
1 => "1.PAUL/JOYTEE RANI MRS",
2 => "2.PAUL/SATTYAJ MSTR(CHD/05NOV04) ",
3 => " ",
4 => "3.PAUL/SATTYAM MSTR(CHD/23JUN08)",
5 => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2\n ",
6 => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
7 => " 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
8 => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2",
);
function filter_array_empty_value($arr){
foreach($arr as $k=>$v){
$v = str_replace("\n","",$v);
$v = str_replace(" ","",$v);
$v = trim($v);
$arr[$k] = $v;
if($v == ""){
unset($arr[$k]);
}
}
return $arr;
}
$arr2 = filter_array_empty_value($arr);
print_r($arr2);
输出是:
Array
(
[1] => 1.PAUL/JOYTEERANIMRS
[2] => 2.PAUL/SATTYAJMSTR(CHD/05NOV04)
[4] => 3.PAUL/SATTYAMMSTR(CHD/23JUN08)
[5] => 4EK004U26JUL6*LHRDXBHK32040063027JULEEK/GAQNL2
[6] => 5EK584U27JUL7*DXBDACHK31315195527JULEEK/GAQNL2
[7] => 6EK583U09SEP2*DACDXBHK31015130509SEPEEK/GAQNL2
[8] => 7EK005U09SEP2*DXBLHRHK31545201509SEPEEK/GAQNL2
)
答案 2 :(得分:0)
那么简单:
public static function filter_array_empty_value($arr){
//Output Array
$output = [];
//Iterate through each item
foreach($arr as $item){
//Make sure, all not printable Chracters are removed (If you want UTF-8 Characters instead of asci use "/[\x00-\x08\x0B\x0C\x0E-\x1F\x80-\x9F]/u"
$item = preg_replace('/[^[:print:]]/', '', $item);
$item = trim($item);
//If item is not empty, append it to the Output
if ($item !== '') {
$output[] = $item;
}
}
return $output ;
}
如评论中所述,我们需要删除不可打印的字符:
答案 3 :(得分:0)
删除数组中的空值array_filter
并重新创建顺序索引使用array_values
,如下所示
$arr =Array (
"0" => "",
"1" => "1.PAUL/JOYTEE RANI MRS",
"2" => "2.PAUL/SATTYAJ MSTR(CHD/05NOV04) ",
"3" => "",
"4" => "3.PAUL/SATTYAM MSTR(CHD/23JUN08)",
"5" => "4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2 ",
"6" => "5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2 ",
"7" => "6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2 ",
"8" => "7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2 ");
print_r(array_values(array_filter($arr)));
<强> 输出 强>
Array
(
[0] => 1.PAUL/JOYTEE RANI MRS
[1] => 2.PAUL/SATTYAJ MSTR(CHD/05NOV04)
[2] => 3.PAUL/SATTYAM MSTR(CHD/23JUN08)
[3] => 4 EK 004 U 26JUL 6*LHRDXB HK3 2040 0630 27JUL E EK/GAQNL2
[4] => 5 EK 584 U 27JUL 7*DXBDAC HK3 1315 1955 27JUL E EK/GAQNL2
[5] => 6 EK 583 U 09SEP 2*DACDXB HK3 1015 1305 09SEP E EK/GAQNL2
[6] => 7 EK 005 U 09SEP 2*DXBLHR HK3 1545 2015 09SEP E EK/GAQNL2
)
答案 4 :(得分:0)
您应该看看如何拆分字符串以获取数组并在那里执行,但这很简单:
$array = preg_grep('/[^\s]+/', $array);
如果还有其他不可见的字符,则将它们添加到字符类[]
中。
如果您需要重新索引:
$array = array_values(preg_grep('/[^\s]+/', $array));