note 99933 added to function.create-function

From: Date: Wed, 15 Sep 2010 20:53:17 +0000
Subject: note 99933 added to function.create-function
Groups: php.notes 
Request: Send a blank email to php-notes+get-172586@lists.php.net to get a copy of this message
The function takes as arguments the tree array, the category id on which i want the retrieval, and the depth level for which i would like the subcategories ids returned. this saves me a LOT of time on large adjacency list trees. $ctg_tree = array( '1'=>array('parent_id'=>0, 'children'=>array( '2'=>array('parent_id'=>1, 'children'=>array( '3'=>array('parent_id'=>2))), '4'=>array('parent_id'=>1, 'children'=>array( '5'=>array('parent_id'=>4), '6'=>array('parent_id'=>4)))))); function getSubCategories($ctg_tree, $id, $level = null) { $getBranchIds = create_function( '$tree, $ctg_id, $level = null, $arr = null,'. ' $push = false, $getBranchIds',' if(!$arr) { $arr = array(); } if(intval($level) && $push){ $level--; } foreach($tree as $key=>$ctg) { if($push) { $arr[] = $key; } if($ctg_id == $key) { $start = true; $push = true; } if(count($ctg[\'children\']) && (intval($level) || $level === null)) { $getBranchIds( $ctg[\'children\'],$ctg_id,$level,&$arr,$push,$getBranchIds ); } if($start) return $arr; } return $arr;'); return $getBranchIds( $ctg_tree, $id, $level, null, false, $getBranchIds ); } print_r(getSubCategories($ctg_tree, 4, 1)); Array ( [0] => 5 [1] => 6 ) print_r(getSubCategories($ctg_tree, 1, 1)); Array ( [0] => 2 [1] => 4 ) print_r(getSubCategories($ctg_tree, 1)); Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 ) ---- Server IP: 69.147.83.197 Probable Submitter: 93.113.220.253 ---- Manual Page -- http://www.php.net/manual/en/function.create-function.php Edit -- https://master.php.net/note/edit/99933 Del: integrated -- https://master.php.net/note/delete/99933/integrated Del: useless -- https://master.php.net/note/delete/99933/useless Del: bad code -- https://master.php.net/note/delete/99933/bad+code Del: spam -- https://master.php.net/note/delete/99933/spam Del: non-english -- https://master.php.net/note/delete/99933/non-english Del: in docs -- https://master.php.net/note/delete/99933/in+docs Del: other reasons-- https://master.php.net/note/delete/99933 Reject -- https://master.php.net/note/reject/99933 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172586) next »