Re: counting nested array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 8/17/05, Ing. Josué Aranda <josuearanda@xxxxxxxxx> 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;
   }

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux