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.
This function effectively applies filter_recursive with a provided callback
And you'd use it this way:
This might help someone
From the PHP
array_filter
documentation: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.
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