list ul recursive php code example

Example 1: list ul recursive php

/**
 * @param array $array
 * @param array $classes
 * @param int $level
 * @return string
 */
function buildHtmlList($array,$classes='',$level=0)
{
    $out = '<ul>';
    if(!empty($classes)) {
        $out = '<ul'.$classes[$level].'>';
    }
    foreach($array as $key => $value) {
        if( is_array($value) ) {
            $i = $level+1;
            $out .= "<li>" . $key ;
            $out .= $this->buildHtmlList($value,$classes,$i);
            $out .= '</li>';
            continue;
        } else {
            $out .= "<li>" . $array[$key] . "</li>";
        }
    }
    $out .= "</ul>";
    return $out;
}

Example 2: list ul recursive php

/**
     * @param $array
     * @return string
     */
    function buildHtmlList($array)
    {
        $out = '<ul>';
        foreach($array as $key => $v) {
            if( is_array($v) ) {
                $out .= '<li>' . $key ;
                $out  .= buildHtmlList($v);
                $out .= '</li>';
                continue;
            } else {
                $out .= '<li>' . $array[$key] . '</li>';
            }
        }
        $out .= '</ul>';
        return $out;
    }

Tags:

Php Example