Repository navigation
Expand file tree
/
Copy pathtree.php
More file actions
113 lines (91 loc) · 3.11 KB
/
Copy pathtree.php
File metadata and controls
113 lines (91 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
//二级树型结构
class Tree {
//用于存放实例化的对象
static private $_instance = null;
//公共静态方法获取实例化的对象
static public function getInstance() {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
}
return self::$_instance;
}
//私有克隆
private function __clone() {}
//私有构造
private function __construct() {}
public function getTree(Array $_all, $_id) {
foreach ($_all as $_key=>$_value) {
if($_value->sid == 0){
$_mainNav[] = $_value;
}else{
$_childNav[] = $_value;
// foreach($_childNav as $_skey=>$_svalue){
// if($_svalue->id == $_value->sid){
// $_sonNav[] = $_svalue;
// }
// }
}
if ($_value->id == $_id) {
$_resultNav[0] = $_value;
$_resultNav[0]->sait[$_resultNav[0]->id] = $_resultNav[0]->name;
}
if ($_value->sid == $_id) {
$_resultNav[0]->child[] = $_value;
// var_dump($_resultNav[0]->child[0]);exit();
foreach($_resultNav[0]->child as $_skey=>$_svalue){
if($_svalue->id == $_value->sid){
// var_dump($_resultNav[0]->child);exit();
$_resultNav[0]->child[0]->son[]= $_svalue;
}
}
}
}
if ($_resultNav[0]->sid != 0) {
foreach ($_mainNav as $_key=>$_value) {
if ($_resultNav[0]->sid == $_value->id) {
$_child = $_resultNav;
$_resultNav[0] = $_value;
$_resultNav[0]->sait[$_resultNav[0]->id] = $_resultNav[0]->name;
$_resultNav[0]->sait[$_child[0]->id] = $_child[0]->name;
}
}
foreach ($_childNav as $_key=>$_value) {
if ($_resultNav[0]->id == $_value->sid) {
$_resultNav[0]->child[] = $_value;
}
}
}
//var_dump($_resultNav);exit();
return $_resultNav;
}
function getAllTree($list,$pk='id',$pid='sid',$child='_child',$root=0){
$tree=array();
$packData=array();
foreach ($list as $data) {
$packData[$data[$pk]] = $data;
}
foreach ($packData as $key =>$val){
if($val[$pid]==$root){//代表跟节点
$tree[]= $packData[$key];
}else{
//找到其父类
$packData[$val[$pid]][$child][]= $packData[$key];
}
}
return $tree;
}
// function getTree($items) {
//
// $tree = array(); //格式化好的树
// foreach ($items as $item){
//
// if (isset($items[$item['sid']])) {
// $items[$item['sid']]['son'][] = &$items[$item['id']];
// }else{
// $tree[] = &$items[$item['id']];
// }
// }
// return $tree;
// }
}