PHP:从多维数组中取出空数组元素 [英] PHP: Remove empty array elements from a multidimensional array
问题描述
我似乎无法找到一个简单的,直接的解决方案,在PHP去除数组空元素的老问题。
I can't seem to find a simple, straight-forward solution to the age-old problem of removing empty elements from arrays in PHP.
我的阵列可能是这样的:
My array may look like this:
Array ( [0] => Array ( [Name] => [EmailAddress] => ) )
(等,如果有更多的数据,虽然有可能不...)
(And so on, if there's more data, although there may not be...)
如果它看起来像上述情况,我希望它成为的完全是空的后,我已经处理了。因此,的print_r($数组);
将输出:
If it looks like the above, I want it to be completely empty after I've processed it. So print_r($array);
would output:
Array ( )
如果我运行
$arrayX = array_filter($arrayX);
我仍然可以在相同 的print_r
输出。无论我已经看了表明,这是去除PHP5空数组元素的最简单的方法,但是。
I still get the same print_r
output. Everywhere I've looked suggests this is the simplest way of removing empty array elements in PHP5, however.
我也试过
$arrayX = array_filter($arrayX,'empty_array');
但我得到了以下错误:警告:array_filter()[function.array过滤器]:第二个参数,'empty_array',应该是一个有效的回调
我在做什么错了?
推荐答案
请尝试使用 array_map()
应用过滤器每个数组中的 $阵列
:
$array = array_map('array_filter', $array);
演示: HTTP://$c$cpad.org/FdfY5aqj
这篇关于PHP:从多维数组中取出空数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!