php - Recursion and passing by reference -


i have tree of categories of following structure:

[6] => array     (         [id] => 6         [name] => computers         [productcount] => 0         [children] => array             (                 [91] => array                     (                         [id] => 91                         [name] => notebook                         [productcount] => 5                         [children] => array                             (                             )                     )                  [86] => array                     (                         [id] => 86                         [name] => desktop                         [productcount] => 0                         [children] => array                             (                             )                     )             )     ) 

beside subcategory, each category may contain products (like folder may contain subfolders , files).

i'm trying write recursive function want take array reference , strip both leaf categories [productcount] = 0 , parent categories contain such empty nodes. in other words, after processing want have categories hold products on sublevels.

i've wrote code, debugging , doesn't strip empty nodes. may i'm not using references properly. please, me fix it, if possible.

    function prunetree( & $node) {     if ( ! $node['children'] && ! $node['productcount']) {         unset($node);     }     if ( ! empty($node['children'])) {         foreach ($node['children'] $key => $child) {             prunetree($node['children'][$key]);         }     }     return; } 

you change parameter in function take array of nodes instead of single node. changes recursion slightly, , prevents need pass along key:

function prunetree(&$nodes) {     foreach ($nodes $key => $node) {         if (!$node['children'] && !$node['productcount']) {             unset($nodes[$key]);         } elseif (!empty($node['children'])) {             prunetree($nodes[$key]['children']);             // line checks if children have been pruned away:             if (empty($nodes[$key]['children'])) {                 unset($nodes[$key]);             }         }     } } 

also, added check ensures if child nodes pruned, parent (now, leaf) node gets pruned.

hope helps!


test data:

$data = array(     6 => array(         'id' => 6,         'name' => 'computers',         'productcount' => 0,         'children' => array(             91 => array(                 'id' => 91,                 'name' => 'notebook',                 'productcount' => 5,                 'children' => array()             ),             86 => array(                 'id' => 86,                 'name' => 'desktop',                 'productcount' => 0,                 'children' => array()             )         )     ) ); 

the call:

prunetree($data); echo '<pre>'; print_r($data); echo '</pre>'; 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -