php - Calculating Treversal Tree items level -
i have database has category names. columns id | categoryname | parentid
. i'm using row_array()
codeigniter model, , want add level key row array. have helper function recursively calculates category levels. wrote helper function in following code segment:
function treegenerate($arrs, $parent_id=0, $level=0) { foreach($arrs $arr){ if($arr["categoryparent"]!=0){ $level+=1; } $arr["level"] = $level; treegenerate($arrs,$arr["categoryparent"],$level); } return $arrs; }
but gives error: "allowed memory size of 33554432 bytes exhausted (tried allocate 523800 bytes)". how can fix problem?
just thoughts without knowing code:
- is possible add level keys starting trunk? might save repetition.
- can move foreach outside of treegenerate?
like:
function treegenerate($arr,$level=0) { if($arr["categoryparent"]!=0){ $level +=1; } else return $level; } treegenerate($arr["categoryparent"],$level);; } foreach ($arrs $arr) $arr['level'] = treegenerate($arr);
Comments
Post a Comment