php数组去掉空值的方法:首先创建一个php示例文件;然后通过“array_filter($entry)”方法过滤数组中的空值;最后通过print打印结果即可。
推荐:《php视频教程》
php过滤数组中的0、null、false和''等空值
php对数组的操作已经一套非常完整的函数,其中就包括过滤空值。
要过滤数组中的所有值为空的元素,可直接用 array_filter() 函数。例如:
$entry = array( 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '', 5 => 0 ); print_r(array_filter($entry)); array( 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '', 5 => 0 );print_r(array_filter($entry));
以上代码会输出:
array( [0] => foo [2] => -1)( [0] => foo [2] => -1)
可以看到,array_filter()函数把所有等值为false的元素全部过滤了。
以上就是php数组去掉空值的方法的详细内容。