删除数组中的空值元素


Delete empty value element in array

Array
    (
      [0] => 0   //value is int 0 which isn;t empty value
      [1] =>     //this is empty value
      [2] =>     //this is empty value
    )

我想使上面的数组如下所示,谁能帮我?

非常感谢

Array
    (
      [0] => 0
    )

您可以使用array_filter删除空值(null,false,'',0(:

array_filter($array);

如果您不想从阵列中删除0,请参阅 @Sabari 的答案:

array_filter($array,'strlen');

您可以使用:

要仅删除空值,请执行以下操作:

$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');

要删除错误值:

$new_array_without_nulls = array_filter($array_with_nulls);

希望这对:)有所帮助

array_filter($array, function($var) {
    //because you didn't define what is the empty value, I leave it to you
    return !is_empty($var);
});
这是

array_filter的典型案例。首先需要定义一个函数,该函数返回TRUE是否应保留该值,并FALSE是否应将其删除:

function preserve($value)
{
    if ($value === 0) return TRUE;
    return FALSE;
}
$array = array_filter($array, 'preserve');

然后,在回调函数(此处preserve(中指定哪些为空,哪些不为空。你没有具体写你的问题,所以你需要自己做。

快速查找数字的方法 零 (0(

    var_dump(  
            array_filter( array('0',0,1,2,3,'text') , 'is_numeric'  )
        );
/* 
print :
array (size=5)
  0 => string '0' (length=1)
  1 => int 0
  2 => int 1
  3 => int 2
  4 => int 3
*/