如何从PHP中的数组中删除空值和空值?

Ram*_*mal 0 php arrays

我想从$listValues数组中删除空值和空值.在这里我使用了删除空值array_filter.示例代码:

$listValues = array("one", "two", "null","three","","four","null");
$resultValues = array_filter($listValues);

echo "<pre>";
print_r($resultValues);
echo "</pre>";
Run Code Online (Sandbox Code Playgroud)

结果:

Array ( [0] => one [1] => two [2] => null [3] => three [5] => four [6] => null ) 
Run Code Online (Sandbox Code Playgroud)

但我想要

Array ( [0] => one [1] => two [3] => three [5] => four ) 
Run Code Online (Sandbox Code Playgroud)

任何建议都非常感谢.

Vig*_*n S 6

试试这个:使用array_diff()函数比较两个(或更多)数组的值,并返回差异.删除null和"".如果您需要删除更多字段,请在数组中添加该值

<?php
$listValues = array("one", "two", "null","three","","four","null");
echo "<pre>";
$a=array_values(array_diff($listValues,array("null","")));
print_r($a);
echo "</pre>";
?>
Run Code Online (Sandbox Code Playgroud)

输出:

Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
)
Run Code Online (Sandbox Code Playgroud)

参考 http://www.w3schools.com/php/func_array_diff.asp