I have an array which holds some categories and their info. The categories have subcategories also. Each category has a number of wallpapers attached. What I;m trying to do is build some counter to specify exactly how many subcategories and wallpapers each category has. No matter where in the tree.... I'm sure it can be done recursively somehow but I'm pulling my hair out since last night. I would really appreciate some help. Here's the code till now. You can run it directly as it will print the whole tree: <?php function printr ( $data ) { echo '<pre>' . print_r ( $data, TRUE ) . '</pre>';die (); } $categories = array ( 1 => array ( 'ID' => 1, 'subcategories' => 1, 'wallpapers' => 11, 'id_parent' => 0 ), 2 => array ( 'ID' => 2, 'subcategories' => 3, 'wallpapers' => 5, 'id_parent' => 1 ), 3 => array ( 'ID' => 3, 'subcategories' => 1, 'wallpapers' => 2, 'id_parent' => 2 ), 4 => array ( 'ID' => 4, 'subcategories' => 0, 'wallpapers' => 7, 'id_parent' => 2 ), 5 => array ( 'ID' => 5, 'subcategories' => 0, 'wallpapers' => 1, 'id_parent' => 2 ), 6 => array ( 'ID' => 6, 'subcategories' => 0, 'wallpapers' => 3, 'id_parent' => 0 ), 7 => array ( 'ID' => 7, 'subcategories' => 0, 'wallpapers' => 2, 'id_parent' => 0 ), 8 => array ( 'ID' => 8, 'subcategories' => 0, 'wallpapers' => 5, 'id_parent' => 0 ), 9 => array ( 'ID' => 9, 'subcategories' => 0, 'wallpapers' => 8, 'id_parent' => 0 ), 10 => array ( 'ID' => 10, 'subcategories' => 0, 'wallpapers' => 5, 'id_parent' => 0 ), 11 => array ( 'ID' => 11, 'subcategories' => 0, 'wallpapers' => 1, 'id_parent' => 0 ), 12 => array ( 'ID' => 12, 'subcategories' => 0, 'wallpapers' => 2, 'id_parent' => 0 ), 13 => array ( 'ID' => 13, 'subcategories' => 0, 'wallpapers' => 5, 'id_parent' => 0 ), 14 => array ( 'ID' => 14, 'subcategories' => 0, 'wallpapers' => 0, 'id_parent' => 0 ), 15 => array ( 'ID' => 15, 'subcategories' => 0, 'wallpapers' => 1, 'id_parent' => 0 ), 16 => array ( 'ID' => 16, 'subcategories' => 0, 'wallpapers' => 1, 'id_parent' => 3 ) ); function parse_tree ( $items, $parent = 0 ) { $nodes = array (); foreach ( $items as $id => $attributes ) { if ( $attributes [ 'id_parent' ] == $parent ) { $nodes [ $id ] = $attributes; $nodes [ $id ] [ 'children' ] = parse_tree ( $items, $id ); } } return $nodes; } printr ( parse_tree ( $categories ) ); ?> PHP:
couldn't you just count the array: $nodes [ $id ] [ 'children' ] = parse_tree ( $items, $id ); $childrenCount = count($nodes [ $id ] [ 'children' ] = parse_tree ( $items, $id )); PHP:
This won't get the number of wallpapers per each category. The same thing must happen with the subcategories. It's ok to count the children recursive and get the number but what about the wallpapers nr?