note 99933 modified in function.create-function by danbrown
| From: | danbrown@php.net | Date: | Wed, 15 Sep 2010 20:58:32 +0000 |
| Subject: | note 99933 modified in function.create-function by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-172587@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.
<?php
$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
)
<?php
print_r(getSubCategories($ctg_tree, 1));
?>
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
[4] => 6
)
--was--
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
)
http://php.net/manual/en/function.create-function.php