Share Two Tree array constructors that do not require recursive implementation. if you need them, give a reference. Non-recursive tree array constructor, code:
$ Item) {if ($ item [$ pid]) {if (! Isset ($ t [$ item [$ pid] ['parent'] [$ item [$ pid]) $ t [$ item [$ id] ['parent'] [$ item [$ pid] = & $ t [$ item [$ pid];} return $ t;}/***** create a subnode tree array * parameter ** $ ar array, data organized in the list of Joins * $ id array as the primary key subscript or associated key name * $ pid * array as the parent key subscript or associated key name * return multi-dimensional array ** /function find_child ($ ar, $ id = 'id', $ pid = 'pid ') {foreach ($ ar as $ v) $ t [$ v [$ id] = $ v; foreach ($ t as $ k =>$ item) {if ($ item [$ pid]) {$ t [$ item [$ pid] ['Ch Ild'] [$ item [$ id] = & $ t [$ k] ;}} return $ t ;} $ data = array ('id' => 1, 'parent' => 0, 'name' => 'grandpa'), array ('id' => 2, 'parent' => 1, 'name' => 'Father '), array ('id' => 3, 'parent' => 1, 'name' => 'Uncle '), array ('id' => 4, 'parent' => 2, 'name' => 'your '), array ('id' => 5, 'parent' => 4, 'name' => 'son'); $ p = find_parent ($ data, 'id ', 'parent'); $ c = find_child ($ data, 'id', 'parent'); print_r ($ c); e Xit;?> |