将数组转换为无序列表(ul-li)

7
我有一个像这样的数组。
Array
(
    [0] => Array
        (
            [name] => Region
            [id] => 3
            [parent_id] => 2
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Asia
                            [id] => 4
                            [parent_id] => 3
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Central Asia
                                            [id] => 6621
                                            [parent_id] => 4
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => Afghanistan
                                                            [id] => 5
                                                            [parent_id] => 6621
                                                            [children] => Array
                                                                (
                                                                    [0] => Array
                                                                        (
                                                                            [name] => Balkh
                                                                            [id] => 6
                                                                            [parent_id] => 5
                                                                            [children] => Array
                                                                                (
                                                                                    [0] => Array
                                                                                        (
                                                                                            [name] => Mazar-e-Sharif
                                                                                            [id] => 7
                                                                                            [parent_id] => 6
                                                                                        )

                                                                                )

                                                                        )

                                                                    [1] => Array
                                                                        (
                                                                            [name] => Kabol
                                                                            [id] => 10
                                                                            [parent_id] => 5
                                                                        )

                                                                    [2] => Array
                                                                        (
                                                                            [name] => Qandahar
                                                                            [id] => 12
                                                                            [parent_id] => 5
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                    [1] => Array
                                        (
                                            [name] => Middle East
                                            [id] => 6625
                                            [parent_id] => 4
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [name] => Armenia
                                                            [id] => 14
                                                            [parent_id] => 6625
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

现在我想将这个数组转换为树形式的,但输出结果很奇怪。

例如,我想要像这样的Region

<li id=3 parent_id=2 > Region </li>

我希望在属性中使用idparent_id。PHP函数如下:

    function olLiTree($tree)
    {
        echo '<ul>';
        foreach($tree as $key => $item) {
            if (is_array($item)) {
                echo '<li>', $key;
                olLiTree($item);
                echo '</li>';
            } else {
                echo '<li>', $item, '</li>'; 
            }
        }
        echo '</ul>';
    }

区域的输出结果是从上述函数中得到的。

<ul>
    <li>0
        <ul>
            <li>Region</li>
            <li>3</li>
            <li>2</li>
            <li>children
                <ul>

1
你能具体说明一下你目前得到的输出吗? - Miro Markaravanes
1
你能否编辑你的问题并添加输出结果? - TaLha Khan
你的foreach循环不正确。你必须将$item['children']作为属性传递给函数,使其成为递归函数。 - Jithin
3个回答

10

也许这就是你寻找的递归。

function olLiTree( $tree ) {
    echo '<ul>';
    foreach ( $tree as $item ) {
        echo "<li id=\"$item[id]\" parent_id=\"$item[parent_id]\" > $item[name] </li>";
        if ( isset( $item['children'] ) ) {
            olLiTree( $item['children'] );
        }
    }
    echo '</ul>';
}

1
我认为这个是正确的:

function olLiTree($tree) {
    echo "<ul>";
    foreach ($tree as $v) {
        echo "<li id='{$v['id']}' parent_id='{$v['parent_id']}'>{$v['name']}</li>";
        if ($v['children'])
            olLiTree($v['children']);
    }
    echo "</ul>";
}

输出:

    地区
      亚洲
        中央亚洲
          阿富汗
            巴尔赫省
              马扎里沙里夫
            喀布尔坎大哈
        中东
          亚美尼亚

1

试试这个:


/**
 * requires the following keys: id, parent_id, name;
 * may contain the following key: children
 *
 * @param  array   $array
 * @return string  ul-li html
 */
function arrayToHtml( array $array ) : string
{
    $html = '';
    if(count($array)) {
        $html .= '<ul>';
        foreach ( $array as $value ) {
            if (is_array($value)) {
                $idString = 'id="' . ($value['id'] ?? '0') .'"';
                $parentIdString = 'parent_id="' . ($value['parent_id'] ?? '0') . '"';
                $attributes = $idString . ' ' . $parentIdString;
                $value['children'] = $value['children'] ?? [];
                $value['name'] = $value['name'] ?? 'error';
                $html .= '<li ' . $attributes . '>' . $value['name'] . ':' . $this->arrayToHtml($value['children']) . '</li>';
            }
        }
        $html .= '</ul>';
    }

    return $html;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接