1.
2.
3.
function arrayToTreeArray($rows)
{
if(!is_array($rows) || empty($rows) ){
return false;
}
// $rows = array(); //stores all the database rows that are to be converted into a tree
$tree = array(); //stores the tree
$tree_index = array(); //an array used to quickly find nodes in the tree
$id_column = "idx"; //The column that contains the id of each node
$parent_column = "pidx"; //The column that contains the id of each node's parent
$text_column = "title"; //The column to display when printing the tree to html
//build the tree - this will complete in a single pass if no parents are defined after children
// vp(count($rows) );die();
// while(count($rows) > 0){
foreach($rows as $row_id => $row){
$row_id = $row[$id_column];
if($row[$parent_column]){
if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){
unset($rows[$row_id]);
}
else{
if(array_key_exists($row[$parent_column], $tree_index)){
$parent = & $tree_index[$row[$parent_column]];
$parent['children'][$row_id] =$row;
$parent['children'][$row_id]["children"] = array();
$tree_index[$row_id] = & $parent['children'][$row_id];
unset($rows[$row_id]);
}
}
}
else{
$tree[$row_id] = $row;
$tree[$row_id]["children"] = array();
$tree_index[$row_id] = & $tree[$row_id];
unset($rows[$row_id]);
}
}
// }
return $tree;
}
{
if(!is_array($rows) || empty($rows) ){
return false;
}
// $rows = array(); //stores all the database rows that are to be converted into a tree
$tree = array(); //stores the tree
$tree_index = array(); //an array used to quickly find nodes in the tree
$id_column = "idx"; //The column that contains the id of each node
$parent_column = "pidx"; //The column that contains the id of each node's parent
$text_column = "title"; //The column to display when printing the tree to html
//build the tree - this will complete in a single pass if no parents are defined after children
// vp(count($rows) );die();
// while(count($rows) > 0){
foreach($rows as $row_id => $row){
$row_id = $row[$id_column];
if($row[$parent_column]){
if((!array_key_exists($row[$parent_column], $rows)) and (!array_key_exists($row[$parent_column], $tree_index))){
unset($rows[$row_id]);
}
else{
if(array_key_exists($row[$parent_column], $tree_index)){
$parent = & $tree_index[$row[$parent_column]];
$parent['children'][$row_id] =$row;
$parent['children'][$row_id]["children"] = array();
$tree_index[$row_id] = & $parent['children'][$row_id];
unset($rows[$row_id]);
}
}
}
else{
$tree[$row_id] = $row;
$tree[$row_id]["children"] = array();
$tree_index[$row_id] = & $tree[$row_id];
unset($rows[$row_id]);
}
}
// }
return $tree;
}
2.
$tree = array (
obj(parentseq:0, menuseq:1, name:'aaa', subMenu:obj(array) ),
obj(parentseq:1, menuseq:2, name:'aaa', subMenu:obj(array) ),
obj(parentseq:1, menuseq:3, name:'aaa', subMenu:obj(array) ),
obj(parentseq:3, menuseq:4, name:'aaa', subMenu:obj(array) ),
);
$menu = array();
$ref = array();
foreach( $tree as $k => $d ) {
//$d['children'] = array();
if( isset( $ref[ $d->getParentMenuSeq() ] ) ) { // we have a reference on its parent
$ref[ $d->getParentMenuSeq() ]->setGNBSubMenu($d,$d->getGNBMenuSeq());
$ref[ $d->getGNBMenuSeq() ] =& $ref[ $d->getParentMenuSeq() ]->getGNBSubMenu($d->getGNBMenuSeq());
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d->getGNBMenuSeq() ] = $d;
$ref[ $d->getGNBMenuSeq() ] =& $menu[ $d->getGNBMenuSeq() ];
}
}
/*
$tree = array (
Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1),
Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2),
Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2),
Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1),
Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2),
Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3),
Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2),
);
$menu = array();
$ref = array();
foreach( $tree as $d ) {
$d['children'] = array();
if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
$ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
}
}
*/
/*
$tree = array (
Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3, 'Sub'=>Array()),
Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
);
$menu = array();
$ref = array();
foreach( $tree as $k => $d ) {
//$d['children'] = array();
if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
$ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ];
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
}
}
*/
obj(parentseq:0, menuseq:1, name:'aaa', subMenu:obj(array) ),
obj(parentseq:1, menuseq:2, name:'aaa', subMenu:obj(array) ),
obj(parentseq:1, menuseq:3, name:'aaa', subMenu:obj(array) ),
obj(parentseq:3, menuseq:4, name:'aaa', subMenu:obj(array) ),
);
$menu = array();
$ref = array();
foreach( $tree as $k => $d ) {
//$d['children'] = array();
if( isset( $ref[ $d->getParentMenuSeq() ] ) ) { // we have a reference on its parent
$ref[ $d->getParentMenuSeq() ]->setGNBSubMenu($d,$d->getGNBMenuSeq());
$ref[ $d->getGNBMenuSeq() ] =& $ref[ $d->getParentMenuSeq() ]->getGNBSubMenu($d->getGNBMenuSeq());
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d->getGNBMenuSeq() ] = $d;
$ref[ $d->getGNBMenuSeq() ] =& $menu[ $d->getGNBMenuSeq() ];
}
}
/*
$tree = array (
Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1),
Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2),
Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2),
Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1),
Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2),
Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3),
Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2),
);
$menu = array();
$ref = array();
foreach( $tree as $d ) {
$d['children'] = array();
if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
$ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['children'][ $d['CategoryID'] ];
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
}
}
*/
/*
$tree = array (
Array ('CategoryID' => 1, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
Array ('CategoryID' => 2, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 3, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 4, 'ParentID' => 0, 'Depth' => 1, 'Sub'=>Array()),
Array ('CategoryID' => 5, 'ParentID' => 2, 'Depth' => 2, 'Sub'=>Array()),
Array ('CategoryID' => 6, 'ParentID' => 3, 'Depth' => 3, 'Sub'=>Array()),
Array ('CategoryID' => 7, 'ParentID' => 1, 'Depth' => 2, 'Sub'=>Array()),
);
$menu = array();
$ref = array();
foreach( $tree as $k => $d ) {
//$d['children'] = array();
if( isset( $ref[ $d['ParentID'] ] ) ) { // we have a reference on its parent
$ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $ref[ $d['ParentID'] ]['Sub'][ $d['CategoryID'] ];
} else { // we don't have a reference on its parent => put it a root level
$menu[ $d['CategoryID'] ] = $d;
$ref[ $d['CategoryID'] ] =& $menu[ $d['CategoryID'] ];
}
}
*/
3.
function array_stack (&$a, $p = '@parent', $c = '@children')
{
$l = $t = array();
foreach ($a AS $key => $val):
if (!$val[$p]) $t[$key] =& $l[$key];
else $l[$val[$p]][$c][$key] =& $l[$key];
$l[$key] = (array)$l[$key] + $val;
endforeach;
return $a = array('tree' => $t, 'leaf' => $l);
}
$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
# ^-----------------------v Link @parent value to key.
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');
echo '<pre>';
print_r ( array_stack($node) );
{
$l = $t = array();
foreach ($a AS $key => $val):
if (!$val[$p]) $t[$key] =& $l[$key];
else $l[$val[$p]][$c][$key] =& $l[$key];
$l[$key] = (array)$l[$key] + $val;
endforeach;
return $a = array('tree' => $t, 'leaf' => $l);
}
$node = array();
$node[1] = array('@parent' => 0, 'title' => 'I am node 1.');
# ^-----------------------v Link @parent value to key.
$node[2] = array('@parent' => 1, 'title' => 'I am node 2.');
$node[3] = array('@parent' => 2, 'title' => 'I am node 3.');
$node[4] = array('@parent' => 1, 'title' => 'I am node 4.');
$node[5] = array('@parent' => 4, 'title' => 'I am node 5.');
echo '<pre>';
print_r ( array_stack($node) );
'개발 > PHP' 카테고리의 다른 글
[보안] php취약점 자동공격 봇 막기 (0) | 2012.08.17 |
---|---|
fsockopen 으로 원격지 xml 파일 파싱하기 (0) | 2012.06.27 |
constant() 함수, 상수값을 변수명($사용)으로 가져올때... (0) | 2010.09.03 |
mysql 연동시 my.ini, php.ini에 utf-8 설정해도 한글 깨질때... (0) | 2010.07.03 |
PHP Zend Framework (0) | 2010.06.18 |