Skip to content

Instantly share code, notes, and snippets.

@ann71727
Last active January 7, 2019 14:07
Show Gist options
  • Save ann71727/99107a00d798b1aa159a92588c7f7a3a to your computer and use it in GitHub Desktop.
Save ann71727/99107a00d798b1aa159a92588c7f7a3a to your computer and use it in GitHub Desktop.
php ( 遞迴 ) 產生樹狀結構,不必重複連接資料庫
<?php
/**
* 樹狀結構 class
*
* @author VECTOR Ann <[email protected]>
* @link https://vector.cool
* @version 1.0.0
*/
$v_tree_demo = [
['id'=>0 , 'parent'=>'' , 'title'=>'level 0' , 'order_num'=>0],
['id'=>1 , 'parent'=>0 , 'title'=>'level 1' , 'order_num'=>0],
['id'=>2 , 'parent'=>0 , 'title'=>'level 2' , 'order_num'=>0],
['id'=>3 , 'parent'=>0 , 'title'=>'level 3' , 'order_num'=>0],
['id'=>4 , 'parent'=>1 , 'title'=>'level 1.1' , 'order_num'=>0],
['id'=>5 , 'parent'=>1 , 'title'=>'level 1.2' , 'order_num'=>0],
['id'=>6 , 'parent'=>1 , 'title'=>'level 1.3' , 'order_num'=>0],
['id'=>7 , 'parent'=>2 , 'title'=>'level 2.1' , 'order_num'=>0],
['id'=>8 , 'parent'=>2 , 'title'=>'level 2.2' , 'order_num'=>0],
['id'=>9 , 'parent'=>2 , 'title'=>'level 2.3' , 'order_num'=>0],
['id'=>10 , 'parent'=>3 , 'title'=>'level 3.1' , 'order_num'=>0],
['id'=>11 , 'parent'=>3 , 'title'=>'level 3.2' , 'order_num'=>0],
['id'=>12 , 'parent'=>3 , 'title'=>'level 3.3' , 'order_num'=>0],
['id'=>13 , 'parent'=>4 , 'title'=>'level 1.2.1' , 'order_num'=>0],
['id'=>14 , 'parent'=>4 , 'title'=>'level 1.2.2' , 'order_num'=>0],
['id'=>15 , 'parent'=>4 , 'title'=>'level 1.2.3' , 'order_num'=>0],
['id'=>16 , 'parent'=>14 , 'title'=>'level 1.2.2.1' , 'order_num'=>0],
['id'=>17 , 'parent'=>14 , 'title'=>'level 1.2.2.2' , 'order_num'=>1],
['id'=>18 , 'parent'=>14 , 'title'=>'level 1.2.2.3' , 'order_num'=>3],
['id'=>19 , 'parent'=>14 , 'title'=>'level 1.2.2.4' , 'order_num'=>2],
['id'=>20 , 'parent'=>14 , 'title'=>'level 1.2.2.5' , 'order_num'=>1],
];
<?php
/**
* get_childs function
* 無限層分類,包裹 HTML ul li 的架構
*
* @param array $items
* @param int $parent_id
* @return string
*
* @author VECTOR Ann <[email protected]>
* @link https://vector.cool
*/
function get_childs($items = array(), $parent_id = 0):string
{
$tree = '<ul>';
foreach ($items as $item) {
if ($item['parent'] === $parent_id) {
$tree .= '<li>';
$tree .= $item['title'];
$tree .= get_childs( $items , $item['id'] );
$tree .= '</li>';
}
}
$tree .= '</ul>';
return $tree;
}
echo get_childs($v_tree_demo);
<?php
/**
* get_childs function
* 無限層分類,包裹 HTML ul li 的架構
*
* @param array $items
* @param int $parent_id
* @return string
*
* @author VECTOR Ann <[email protected]>
* @link https://vector.cool
*/
function get_childs( array $items = array(), int $parent_id = 0 ):string
{
$wrap = "<ul><li><h3>%s</h3>%s</li></ul>";
$tree = '';
foreach ($items as $item) {
if ($item['parent'] === $parent_id) {
$child_tree = get_childs( $items , $item['id'] );
$tree .= sprintf( $wrap , $item['title'] , $child_tree);
}
}
return $tree;
}
echo get_childs($v_tree_demo);
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => level 1
[order_num] => 0
[childs] => Array
(
[0] => Array
(
[id] => 4
[parent] => 1
[title] => level 1.1
[order_num] => 0
[childs] => Array
(
[0] => Array
(
[id] => 13
[parent] => 4
[title] => level 1.2.1
[order_num] => 0
[childs] => Array
(
)
)
[1] => Array
(
[id] => 14
[parent] => 4
[title] => level 1.2.2
[order_num] => 0
[childs] => Array
(
[0] => Array
(
[id] => 16
[parent] => 14
[title] => level 1.2.2.1
[order_num] => 0
[childs] => Array
(
)
)
[1] => Array
(
[id] => 17
[parent] => 14
[title] => level 1.2.2.2
[order_num] => 1
[childs] => Array
(
)
)
[2] => Array
(
[id] => 18
[parent] => 14
[title] => level 1.2.2.3
[order_num] => 3
[childs] => Array
(
)
)
[3] => Array
(
[id] => 19
[parent] => 14
[title] => level 1.2.2.4
[order_num] => 2
[childs] => Array
(
)
)
[4] => Array
(
[id] => 20
[parent] => 14
[title] => level 1.2.2.5
[order_num] => 1
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 15
[parent] => 4
[title] => level 1.2.3
[order_num] => 0
[childs] => Array
(
)
)
)
)
[1] => Array
(
[id] => 5
[parent] => 1
[title] => level 1.2
[order_num] => 0
[childs] => Array
(
)
)
[2] => Array
(
[id] => 6
[parent] => 1
[title] => level 1.3
[order_num] => 0
[childs] => Array
(
)
)
)
)
[1] => Array
(
[id] => 2
[parent] => 0
[title] => level 2
[order_num] => 0
[childs] => Array
(
[0] => Array
(
[id] => 7
[parent] => 2
[title] => level 2.1
[order_num] => 0
[childs] => Array
(
)
)
[1] => Array
(
[id] => 8
[parent] => 2
[title] => level 2.2
[order_num] => 0
[childs] => Array
(
)
)
[2] => Array
(
[id] => 9
[parent] => 2
[title] => level 2.3
[order_num] => 0
[childs] => Array
(
)
)
)
)
[2] => Array
(
[id] => 3
[parent] => 0
[title] => level 3
[order_num] => 0
[childs] => Array
(
[0] => Array
(
[id] => 10
[parent] => 3
[title] => level 3.1
[order_num] => 0
[childs] => Array
(
)
)
[1] => Array
(
[id] => 11
[parent] => 3
[title] => level 3.2
[order_num] => 0
[childs] => Array
(
)
)
[2] => Array
(
[id] => 12
[parent] => 3
[title] => level 3.3
[order_num] => 0
[childs] => Array
(
)
)
)
)
)
<?php
/**
* get_childs_tree function
* ( 遞迴 ) 樹狀結構陣列
*
* @param array $items
* @param int $parent_id
* @return array
*
* @author VECTOR Ann <[email protected]>
* @link https://vector.cool
*/
function get_childs_tree($items = array(), $parent_id = 0):array
{
$results = [];
foreach ($items as $item) {
if ($item['parent'] === $parent_id) {
$id = $item['id'];
$item['childs'] = get_childs_tree($items, $id);
$results[] = $item;
}
}
return $results;
}
print_r(get_childs_tree($v_tree_demo));
<?php
/**
* (遞迴) 取得最大深度 function
*
* @param array $array
* @return int
*
* @author VECTOR Ann <[email protected]>
* @link https://vector.cool
*/
function array_depth(array $array):int {
$max_depth = 1;
foreach ($array as $value) {
if (is_array($value['childs'])) {
$depth = array_depth($value['childs']) + 1;
if ($depth > $max_depth) {
$max_depth = $depth;
}
}
}
return $max_depth;
}
$true = get_childs_tree($v_tree_demo);
echo array_depth($true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment