Php 删除数组中的空值元素

Php 删除数组中的空值元素,php,Php,我想让上面的数组如下,有人能帮我吗 非常感谢 Array ( [0] => 0 //value is int 0 which isn;t empty value [1] => //this is empty value [2] => //this is empty value ) 您可以使用删除空值(null、false、、0): 如果不想从数组中删除0,请参阅@Sabari的答案: array_filt

我想让上面的数组如下,有人能帮我吗

非常感谢

Array
    (
      [0] => 0   //value is int 0 which isn;t empty value
      [1] =>     //this is empty value
      [2] =>     //this is empty value
    )
您可以使用删除空值(null、false、、0):

如果不想从数组中删除
0
,请参阅@Sabari的答案:

array_filter($array);
您可以使用:

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

array_filter($array,'strlen');
要删除假值,请执行以下操作:

$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
希望这有帮助:)

这是一个典型的例子。您首先需要定义一个函数,该函数返回
TRUE
(如果应保留该值)和
FALSE(如果应删除该值):

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

然后在回调函数(此处
preserve
)中指定什么是空的,什么不是空的。您没有在问题中特别写,所以您需要自己写。

快速找到数字也为零(0)的方法


这些空值是什么?它们是假、空、空字符串还是其他什么?那0呢?是整数0还是字符串“0”?对数组使用
var\u dump()
来确定值的类型。不知道第二个参数是可选的,这很好。@Zulkhaery Basrul,数组过滤器会认为值0是空值,所以最终结果是一个空数组,这不是我想要的
$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);
});
function preserve($value)
{
    if ($value === 0) return TRUE;

    return FALSE;
}

$array = array_filter($array, 'preserve');
    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

*/