list ul recursive php

Solutions on MaxInterview for list ul recursive php by the best coders in the world

showing results for - "list ul recursive php"
Yannick
23 Jul 2016
1    
2/**
3 * @param array $array
4 * @param array $classes
5 * @param int $level
6 * @return string
7 */
8function buildHtmlList($array,$classes='',$level=0)
9{
10    $out = '<ul>';
11    if(!empty($classes)) {
12        $out = '<ul'.$classes[$level].'>';
13    }
14    foreach($array as $key => $value) {
15        if( is_array($value) ) {
16            $i = $level+1;
17            $out .= "<li>" . $key ;
18            $out .= $this->buildHtmlList($value,$classes,$i);
19            $out .= '</li>';
20            continue;
21        } else {
22            $out .= "<li>" . $array[$key] . "</li>";
23        }
24    }
25    $out .= "</ul>";
26    return $out;
27}
28
Marley
21 Nov 2018
1   
2    /**
3     * @param $array
4     * @return string
5     */
6    function buildHtmlList($array)
7    {
8        $out = '<ul>';
9        foreach($array as $key => $v) {
10            if( is_array($v) ) {
11                $out .= '<li>' . $key ;
12                $out  .= buildHtmlList($v);
13                $out .= '</li>';
14                continue;
15            } else {
16                $out .= '<li>' . $array[$key] . '</li>';
17            }
18        }
19        $out .= '</ul>';
20        return $out;
21    }
22