Last active
January 5, 2019 21:50
-
-
Save ann71727/7621876b98038f6aa6f07dfe9bd9195b to your computer and use it in GitHub Desktop.
PHP 無限層分類,包裹 HTML ul li 的架構 multi-level category recursion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment