master
fengyuexingzi 8 years ago
parent 10393729e9
commit 84febebf88

@ -6,45 +6,56 @@
* Time: 13:46 * Time: 13:46
*/ */
$arr = [
$test_data = [ ['id'=>1,'title'=>'first','pid'=>0,'level'=>1],
['id' => 0, 'name' => 'A'], ['id'=>2,'title'=>'second','pid'=>0,'level'=>1],
['id' => 1, 'name' => 'a', 'pid' => 0], ['id'=>3,'title'=>'third','pid'=>1,'level'=>2],
['id' => 2, 'name' => 'b', 'pid' => 1], ['id'=>4,'title'=>'fourth','pid'=>1,'level'=>2],
['id' => 3, 'name' => 'c', 'pid' => 1], ['id'=>5,'title'=>'fifth','pid'=>2,'level'=>2],
['id' => 4, 'name' => 'd', 'pid' => 5], ['id'=>6,'title'=>'sixth','pid'=>3,'level'=>3],
['id' => 5, 'name' => 'e', 'pid' => 0], ['id'=>7,'title'=>'seventh','pid'=>6,'level'=>4],
['id' => 6, 'name' => 'e', 'pid' => 4], ['id'=>8,'title'=>'seventh','pid'=>7,'level'=>5],
['id'=>9,'title'=>'seventh','pid'=>8,'level'=>6],
['id'=>10,'title'=>'seventh','pid'=>8,'level'=>6],
['id'=>11,'title'=>'seventh','pid'=>8,'level'=>6],
]; ];
function m_tree($arr, $pid = 0)
{ function tree($arr, $pid = 0, $level = 1){
$tree = []; $child = [];
foreach ($arr as $t) { foreach($arr as $v){
if ($t['id'] == $pid) { // $child:孩子 $v:孩子的孩子 $childNodes:孩子的孩子的孩子,也就是说,在这个函数里,要处理子孙三代
$tree = $t; if($v['pid'] == $pid){ // 得到子节点后递归得到子节点的子节点
} $childNodes = tree($arr, $v['id'], $level + 1); // get childNodes's childNodes
} if($childNodes){
foreach ($arr as $child) { $v['child'] = $childNodes;
if (isset($child['pid']) && $child['pid'] == $tree['id']) { $v['level'] = $level;
$tree['child'][] = $child; }
$child[] = $v; // $v is $child's child
} }
} }
foreach ($tree as $k1 => $v1) { return $child;
if (is_array($v1)) { }
foreach ($v1 as $k2 => $v2) {
$tree['child'][$k2] = m_tree($arr, $v2['id']); $tree = tree($arr,0);
}
function html($tree, $level = 0){
$str = '<ul>';
foreach($tree as $v){
if(isset($v['child'])){
$str .= "<li level=\"{$v['level']}\">{$v['title']}<ul>";
$str .= html($v['child']);
$str .= "</ul></li>";
}else{
$str .= "<li level=\"{$v['level']}\">{$v['title']}</li>";
} }
} }
return $tree; return $str .= '</ul>';
} }
echo '<pre>'; html($tree);
print_r(m_tree($test_data));
echo '</pre>';
die;
function tree($directory) die;
function file_tree($directory)
{ {
$mydir = dir($directory); $mydir = dir($directory);
echo "<ul>\n"; echo "<ul>\n";
@ -57,7 +68,7 @@ function tree($directory)
if ((is_dir($child)) AND ($file != ".") AND ($file != "..") AND ($file != '$RECYCLE.BIN') AND ($file != 'System Volume Information')) { if ((is_dir($child)) AND ($file != ".") AND ($file != "..") AND ($file != '$RECYCLE.BIN') AND ($file != 'System Volume Information')) {
var_dump("directory: " . $child); var_dump("directory: " . $child);
echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n"; echo "<li><font color=\"#ff00cc\"><b>$file</b></font></li>\n";
tree($child); file_tree($child);
} else } else
echo "<li>$file</li>\n"; echo "<li>$file</li>\n";
} }
@ -70,7 +81,7 @@ function getChmod($filepath)
return substr(base_convert(@fileperms($filepath), 10, 8), -4); return substr(base_convert(@fileperms($filepath), 10, 8), -4);
} }
$dirs = tree('/'); $dirs = file_tree('/');
var_dump($dirs); var_dump($dirs);

Loading…
Cancel
Save