Php 删除字符串(1)“&引用;

Php 删除字符串(1)“&引用;,php,foreach,Php,Foreach,我从列表中得到: one "\n" "\n" two "\n" 当我得到列表时,我会爆炸: $list_explode_nl = explode("\n", $list); 变量转储显示: 数组(3){[0]=>string(5)“一”[1]=>string(1)”“[2]=>string(4)“二”} 我的代码: foreach ($list_explode_nl as $k => $v) { if ($v == NULL ||

我从列表中得到:

one     "\n"
        "\n"
two     "\n"
当我得到列表时,我会爆炸:

     $list_explode_nl = explode("\n", $list);
变量转储显示:

数组(3){[0]=>string(5)“一”[1]=>string(1)”“[2]=>string(4)“二”}

我的代码:

  foreach ($list_explode_nl as $k => $v) {
     if ($v == NULL || $v == ' ') {
        unset($list_explode_nl[$k]);
     }
  }
当字符串类似于[1]时,我想去掉一个元素。怎么做?

试试:

foreach ($list_explode_nl as $k => $v) {
    if ($v == NULL || trim($v) == '') {
        unset($list_explode_nl[$k]);
    }
 }
我认为
[1]
元素是一个换行符(或者可能是制表符),因此它不等于
'
。你应该注意它。

试试:

foreach ($list_explode_nl as $k => $v) {
    if ($v == NULL || trim($v) == '') {
        unset($list_explode_nl[$k]);
    }
 }
$list_explode_nl = array_filter(array_map('trim',$list_explode_nl));
我认为
[1]
元素是一个换行符(或者可能是制表符),因此它不等于
'
。你应该照顾好它

$list_explode_nl = array_filter(array_map('trim',$list_explode_nl));

如果您还想重置索引

如果您还想重置索引