将数组转换为Ul-Li

时间:2013-05-30 13:23:31

标签: php arrays function multidimensional-array

我有一个像这样的数组

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
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

现在我想将此数组转换为树形结构中的ul-li

但它给了我奇怪的输出

例如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>

3 个答案:

答案 0 :(得分:9)

也许这是你寻求的递归。

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 :(得分:0)

我认为这是正确的:

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>";
}

输出:

  
    地区
      亚洲
        中央   亚洲
          阿富汗
            Balkh
              Mazar-e-Sharif
            KabolQandahar
        中东
          亚美尼亚

答案 2 :(得分:0)

尝试一下:


/**
 * 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;
}