Skip to content

Instantly share code, notes, and snippets.

@ann71727
Last active January 5, 2019 21:50
Show Gist options
  • Save ann71727/7621876b98038f6aa6f07dfe9bd9195b to your computer and use it in GitHub Desktop.
Save ann71727/7621876b98038f6aa6f07dfe9bd9195b to your computer and use it in GitHub Desktop.
PHP 無限層分類,包裹 HTML ul li 的架構 multi-level category recursion
/**
* 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