Posted by Robin Vickery on 08/17/05 12:04
On 8/17/05, Ing. Josué Aranda <josuearanda@gmail.com> wrote:
> OK this the little function i made to solve this..
>
> function countNested($array){
> foreach($array as $value){
> if(is_array($value))
> $total=$this->countNested($value)+$total;
> }else{
> $total=$total+1;
> }
> }
> return $total;
> }
Looks OK-ish - there's a missing '{' on the third line but apart from
that it should work fine as a class method.
> any optimizations are welcome....
You can simplify the if-block as below, which might save you as much
as a microsecond or two :-)
function countNested($array){
$total = 0;
foreach ($array as $value) {
$total += is_array($value) ? $this->countNested($value) : 1;
}
return $total;
}
Navigation:
[Reply to this message]
|