Reply to Re: [PHP] counting nested array

Your name:

Reply:


Posted by Ing. Josuι Aranda on 08/17/05 05:31

OK this the little function i made to solve this..
[CODE]

function countNested($array){
foreach($array as $value){
if(is_array($value))
$total=$this->countNested($value)+$total;
}else{
$total=$total+1;
}
}
return $total;
}

[/CODE]

any optimizations are welcome....



On 8/16/05, Robin Vickery <robinv@gmail.com> wrote:
> On 8/16/05, Ing. JosuΓ© Aranda <josuearanda@gmail.com> wrote:
> >
> > The number of the branches is not always the same.. (it depends on the
> > query).. when i use count($array, COUNT_RECURSIVE) for nested arrays..
> > it give to me the total including the nodes in the branches ( in this
> > case 28).. now here is the question, how i can get only the last nodes
> > in this case ... exist a easy way to do it?. or its necessary to make
> > a funcion with a bunch of foreach?.. any suggestions are welcome =o)
> > thanks!
>
> If I understand you correctly, you only want the leaves of your tree -
> in your example, that would be 20?
>
> I don't think there's a convenient builtin function that'll do it, but
> it's not hard to write your own:
>
> <?php
>
> function leaf_count($item) {
> $count = 0;
>
> if (!is_array($item)) { return 1; }
>
> foreach ($item as $element) {
> $count += leaf_count($element);
> }
>
> return $count;
> }
>
> print leaf_count($array);
>
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
____________________

JOSUE ARANDA>>>

[Back to original 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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация