如何递归删除数组中的空条目?
我需要删除多级数组上的空条目。现在我可以删除带有空子数组的条目,但不能删除空数组......很困惑,我也是......我认为代码将有助于更好地解释。
/**
*
* This function remove empty entries on arrays
* @param array $array
*/
function removeEmptysFromArray($array) {
$filtered = array_filter($array, 'removeEmptyItems');
return $filtered;
}
/**
*
* This is a Callback function to use in array_filter()
* @param array $item
*/
function removeEmptyItems($item) {
if (is_array($item)) {
return array_filter($item, 'removeEmptyItems');
}
if (!empty($item)) {
return true;
}
}
$raw = array(
'firstname' => 'Foo',
'lastname' => 'Bar',
'nickname' => '',
'birthdate' => array(
'day' => '',
'month' => '',
'year' => '',
),
'likes' => array(
'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'),
'bikes' => array(),
),
);
print_r(removeEmptysFromArray($raw));
此代码将删除nickname
、birthdate
,但不会删除具有空数组的bikes
。
我的问题是...如何删除 bikes
条目?
I need to remove empty entries on multilevel arrays. For now I can remove entries with empty sub-arrays, but not empty arrays... confused, so do I... I think the code will help to explain better.
/**
*
* This function remove empty entries on arrays
* @param array $array
*/
function removeEmptysFromArray($array) {
$filtered = array_filter($array, 'removeEmptyItems');
return $filtered;
}
/**
*
* This is a Callback function to use in array_filter()
* @param array $item
*/
function removeEmptyItems($item) {
if (is_array($item)) {
return array_filter($item, 'removeEmptyItems');
}
if (!empty($item)) {
return true;
}
}
$raw = array(
'firstname' => 'Foo',
'lastname' => 'Bar',
'nickname' => '',
'birthdate' => array(
'day' => '',
'month' => '',
'year' => '',
),
'likes' => array(
'cars' => array('Subaru Impreza WRX STi', 'Mitsubishi Evo', 'Nissan GTR'),
'bikes' => array(),
),
);
print_r(removeEmptysFromArray($raw));
This code will remove nickname
, birthdate
but is not removing bikes
that have an empty array.
My question is... How to remove the bikes
entry?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
试试这个代码:
Try this code:
这是我的解决方案,它将递归地删除精确指定的空值列表:
您可以像这样使用它:
注意,如果您将
[]
作为值之一传递,它会删除空子数组。Here is my solution, it will remove exactly specified list of empty values recursively:
You can use it like this:
Note, it removes empty sub arrays if you pass
[]
as one of values.我认为这应该可以解决你的问题。
I think this should solve your problem.
递归清除空项的多维数组:
单元测试:
在 3v4l.org 进行概念验证
Recursively clear multidimensional array of empty'ish items:
Unit test:
Working proof of concept at 3v4l.org
array_filter()
和empty()
对于提问者的示例输入来说可能是安全的,但它们对于公共一般用途来说并不可靠。这些本机函数会将所有“错误”值视为可过滤/空。请参阅empty()
的文档欲知其贪空性断定的详情。如果这是意图的话,我建议显式检查(特别是数据类型)是否为空字符串。这样您就不会无意中破坏有意义的数据。我选择将
$value
声明为引用,以避免迭代数组的副本 - 这允许我使用$value
而不是 more 来改变/覆盖数组数据详细$array[$key]
(不使用unset()
时。代码:(演示具有各种错误值)
*请注意,此脚本并非设计用于容纳数据有效负载中的对象。
array_filter()
andempty()
may be safe to leverage for the asker's sample input, but they are not reliable for public general use. These native functions will treat all "falsey" values as filterable/empty. See the documentation forempty()
for details on its greedy determination of emptiness.I recommend explicitly checking (data type specifically) for empty string strings if that is the intent. This way you don't inadvertently destroy meaningful data. I am choosing to declare
$value
as a reference to avoid iterating a copy of the array -- this allows me to mutate/overwrite array data with$value
instead of the more verbose$array[$key]
(when not usingunset()
.Code: (Demo with an assortment of falsey values)
*Note that this script is not designed to accommodate objects in the data payload.
我的功能:
My function:
基于递归函数的简单而优雅的解决方案。不喜欢上一个解决方案。答案。删除空子数组/子值:
Simple and elegant solution based on recursive function. Dont liked any solution from the prev. answers. Removes empty sub arrays/sub values:
如果您希望 array_filter 递归工作,则需要确保后续调用可以编辑数组的更深层次的嵌套项。简而言之:您需要通过引用传递它:
If you want array_filter to work recursively, you'll need to make sure that the subsequent calls may edit the deeper nested items of the array. Short: You'll need to pass it by reference: