如何在PHP数组中递归运行array_filter? [英] How to run array_filter recursively in a PHP array?
问题描述
给定以下数组 $ mm
数组
(
[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和空。
应该工作
$ count = array_sum(array_map(function($ item){
return((int)!is_null($ item ['pts_m'])
+ int)!is_null($ item ['pts_mreg'])
+((int)!is_null($ item ['pts_cg']);
},$ array);
或也许
$ count = array_sum(array_map(function($ item){
return array_sum(array_map('is_int',$ item));
},$ array);
如果你想使用 array_filter()
回调)记住,它也把 0
当作 false
,因此它会 0
- 数组中的值。
如果您在5.3之前的版本中使用PHP, a foreach
-loop
$ count =
foreach($ array as $ item){
$ count + =((int)!is_null($ item ['pts_m'])
+((int)!is_null ['pts_mreg'])
+((int)!is_null($ item ['pts_cg']);
}
更新
关于以下意见:
Thx @kc我实际上希望该方法删除false,0,空等等。
当这真的只是你想要的,解决方案也很简单
但现在我不知道,如何解释
我的预期结果将是5。
无论如何,
$ result = array_map('array_filter',$ array);
$ count = array_map('count',$ result)
$ countSum = array_sum($ count);
结果数组看起来像
数组
(
[147] => Array
(
[pts_mreg] => 1
[pts_cg] => 1
)
[158] =>数组
(
)
[159] =>数组
(
[pts_mreg] => 1
[pts_cg] => 1
)
)
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 get3
as result since it is not recursive.
count(array_filter($mm), COUNT_RECURSIVE)
also will not do because I actually need to run thearray_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 be4
.Please note that I am not using any callback so I can exclude false, null and empty.
解决方案Should work
$count = array_sum(array_map(function ($item) { return ((int) !is_null($item['pts_m']) + ((int) !is_null($item['pts_mreg']) + ((int) !is_null($item['pts_cg']); }, $array);
or maybe
$count = array_sum(array_map(function ($item) { return array_sum(array_map('is_int', $item)); }, $array);
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
-loop$count = 0; foreach ($array as $item) { $count += ((int) !is_null($item['pts_m']) + ((int) !is_null($item['pts_mreg']) + ((int) !is_null($item['pts_cg']); }
Update
Regarding the comment below:
Thx @kc I actually want the method to remove false, 0, empty etc
When this is really only, what you want, the solution is very simple too. But now I don't know, how to interpret
My expected result here would be 5.
Anyway, its short now :)
$result = array_map('array_filter', $array); $count = array_map('count', $result); $countSum = array_sum($count);
The resulting array looks like
Array ( [147] => Array ( [pts_mreg] => 1 [pts_cg] => 1 ) [158] => Array ( ) [159] => Array ( [pts_mreg] => 1 [pts_cg] => 1 ) )
这篇关于如何在PHP数组中递归运行array_filter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!