数“真实”二维数组中的值
给定以下数组 $mm
Array
(
[147] => Array
(
[pts_m] =>
[pts_mreg] => 1
[pts_cg] => 1
)
[158] => Array
(
[pts_m] =>
[pts_mreg] =>
[pts_cg] => 0
)
[159] => Array
(
[pts_m] =>
[pts_mreg] => 1
[pts_cg] => 1
)
)
当我运行 count(array_filter($mm))
时,我得到 3
作为结果,因为它不是递归的。
count(array_filter($mm), COUNT_RECURSIVE)
也不会这样做,因为我实际上需要递归地运行 array_filter
,然后计算其结果。
所以我的问题是:在这种情况下如何递归运行 array_filter($mm) ? 我的预期结果是 4
。
请注意,我没有使用任何回调,因此我可以排除 false、null 和empty。
Given the following array $mm
Array
(
[147] => Array
(
[pts_m] =>
[pts_mreg] => 1
[pts_cg] => 1
)
[158] => Array
(
[pts_m] =>
[pts_mreg] =>
[pts_cg] => 0
)
[159] => Array
(
[pts_m] =>
[pts_mreg] => 1
[pts_cg] => 1
)
)
When I run count(array_filter($mm))
I get 3
as result since it is not recursive.
count(array_filter($mm), COUNT_RECURSIVE)
also will not do because I actually need to run the array_filter
recursively, and then count its result.
So my question is: how do I recursively run array_filter($mm)
in this case?
My expected result here would be 4
.
Please note that I am not using any callback so I can exclude false, null and empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
来自 PHP
array_filter
文档:From the PHP
array_filter
documentation:应该可行
或者也许
肯定有更多可能的解决方案。如果您想使用
array_filter()
(不带回调),请记住,它也会将0
视为false
,因此它将删除 数组中的任何0
值。如果您在 5.3 之前的版本中使用 PHP,我将使用
foreach
-loop更新
关于以下评论:
当这真的只是你想要的时,解决方案也很简单。
但现在我不知道如何解释
无论如何,现在它很短:)
结果数组看起来像
Should work
or maybe
There are definitely many more possible solutions. If you want to use
array_filter()
(without callback) remember, that it treats0
asfalse
too and therefore it will remove any0
-value from the array.If you are using PHP in a pre-5.3 version, I would use a
foreach
-loopUpdate
Regarding the comment below:
When this is really only, what you want, the solution is very simple too.
But now I don't know, how to interpret
Anyway, its short now :)
The resulting array looks like
更好的替代方案
一直对我有用的一个实现是这样的:
我注意到有人创建了一个类似的函数,但在我看来,这个函数没有什么优点:
基准
我希望它有所帮助。
A better alternative
One implementation that always worked for me is this one:
I notice that someone had created a similar function except that this one presents, in my opinion, few advantages:
Benchmarks
I hope it helps.
这个函数有效地应用了filter_recursive和提供的回调
,你可以这样使用它:
这可能会帮助某人
This function effectively applies filter_recursive with a provided callback
And you'd use it this way:
This might help someone
我需要一个数组过滤器递归函数来遍历所有节点(包括数组,以便我们有可能丢弃整个数组),所以我想出了这个:
在此处查看更多示例:https://github.com/lingtalfi/Bat/blob/master/ArrayTool.md#filterrecursive
I needed an array filter recursive function that would walk through all nodes (including arrays, so that we have the possibility to discard entire arrays), and so I came up with this:
See more examples here: https://github.com/lingtalfi/Bat/blob/master/ArrayTool.md#filterrecursive
这应该适用于回调和模式支持以及可选的深度支持。
对于嵌套数组,使用
$depth = 0
调用该函数将产生与array_filter
相同的结果。This should work for callback and mode support along with an optional support for depth.
Calling the function with
$depth = 0
for nested arrays, will yield the same result asarray_filter
.以下代码片段不调用任何函数(仅调用语言构造 -
foreach()
),因此效率很高。代码:(演示)
对于函数式方法,将所有真值的映射计数相加。 演示
The following snippet calls no functions (only language constructs --
foreach()
) and therefore will be highly efficient.Code: (Demo)
For a functional-style approach, sum the mapped counts of all truthy values. Demo