Re: [PHP] counting nested array
On 8/17/05, Ing. Josué Aranda [EMAIL PROTECTED] 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
Re: [PHP] counting nested array
hahaha, thanks robin you save some seconds of mi life... it looks more pro with that if... On 8/17/05, Robin Vickery [EMAIL PROTECTED] wrote: On 8/17/05, Ing. Josué Aranda [EMAIL PROTECTED] 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 -- JOSUE ARANDA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] counting nested array
Ing. Josué Aranda wrote: Hi to everyone.. now i have a little problem counting an nested array. Im using it to fill a Java TreeView... it looks like this: [snip] 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! Well, if you do a count of the root, you ought to get the amount of numbers in there, and, with that number of the last item, access that array, do a count() on that, get the last item, and so forth and so on, you could write a small recursive function here to call itself with each new child, getting the last item, down into the structure until X level. Not sure if there's a PHP native function for doing this, though. Perhaps someone else knows. If nothing else, try this way. Regards, Torgny -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] counting nested array
On 8/16/05, Ing. Josué Aranda [EMAIL PROTECTED] 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
Re: [PHP] counting nested array
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 [EMAIL PROTECTED] wrote: On 8/16/05, Ing. Josué Aranda [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php