You are here: Re: [PHP] counting nested array « PHP « IT news, forums, messages
Re: [PHP] counting nested array

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация