嵌套数组到HTML列表

时间:2011-10-06 08:33:16

标签: php html arrays

我有以下数组:

Array
(
    [0] => Array
        (
            [ID] => 1
            [PARENT] => 0
            [NAME] => Your first category
        )
    [1] => Array
        (
            [ID] => 2
            [PARENT] => 1
            [NAME] => Your first forum
        )
    [2] => Array
        (
            [ID] => 4
            [PARENT] => 1
            [NAME] => Another Forum
        )
    [3] => Array
        (
            [ID] => 5
            [PARENT] => 1
            [NAME] => Good Forum
        )
    [4] => Array
        (
            [ID] => 6
            [PARENT] => 0
            [NAME] => Top Forum
        )
    [5] => Array
        (
            [ID] => 7
            [PARENT] => 6
            [NAME] => Sub Forum #1
        )
    [6] => Array
        (
            [ID] => 9
            [PARENT] => 7
            [NAME] => Sub Forum #1-1
        )
    [7] => Array
        (
            [ID] => 10
            [PARENT] => 7
            [NAME] => Sub Forum #1-2
        )
    [8] => Array
        (
            [ID] => 8
            [PARENT] => 6
            [NAME] => Sub Forum #2
        )
)

OK这里我按要求获得了var_export结果:

array (
  0 => 
  array (
    'ID' => '1',
    'PARENT' => '0',
    'NAME' => 'Your first category',
  ),
  1 => 
  array (
    'ID' => '2',
    'PARENT' => '1',
    'NAME' => 'Your first forum',
  ),
  2 => 
  array (
    'ID' => '4',
    'PARENT' => '1',
    'NAME' => 'Another Forum',
  ),
  3 => 
  array (
    'ID' => '5',
    'PARENT' => '1',
    'NAME' => 'Good Forum',
  ),
  4 => 
  array (
    'ID' => '6',
    'PARENT' => '0',
    'NAME' => 'Top Forum',
  ),
  5 => 
  array (
    'ID' => '7',
    'PARENT' => '6',
    'NAME' => 'Sub Forum #1',
  ),
  6 => 
  array (
    'ID' => '9',
    'PARENT' => '7',
    'NAME' => 'Sub Forum #1-1',
  ),
  7 => 
  array (
    'ID' => '10',
    'PARENT' => '7',
    'NAME' => 'Sub Forum #1-2',
  ),
  8 => 
  array (
    'ID' => '8',
    'PARENT' => '6',
    'NAME' => 'Sub Forum #2',
  ),
)

某些数组PARENT值与另一个数组ID值相对应。这有助于我使它们嵌套。但问题是:如何创建一个看起来像这样的HTML列表:

<ul>
    <li id="1">
        Your First Category
        <ul>
            <li id="2">
                Your First Forum
            </li>
            <li id="4">
                Another Forum
            </li>
            <li id="5">
                Good Forum
            </li>
        <ul>
    </li>
    <li id="6">
        Top Forum
        <ul>
            <li id="7">
                Sub Forum #1
                <ul>
                    <li id="9">
                        Sub Forum #1-1
                    </li>
                    <li id="10">
                        Sub Forum #1-2
                    </li>
                </ul>
            </li>
            <li id="7">
                Sub Forum #2
            </li>
        <ul>
    </li>
</ul>

请问好吗?

2 个答案:

答案 0 :(得分:7)

我会做什么:

  1. 按其父ID - $menu[$parentID] = array($child, $child, ...)
  2. 分组菜单(?)项目
  3. 使用递归函数为每个父级生成菜单 - makeMenu($menu, $parentID)
  4. 这将允许您访问每个节点两次 - 第一次,重新排序数组时,第二次,打印节点时。因此,无论是小型还是大型阵列,它都能很好地工作。

    来源:

    $data = array(
        0 => array(
            'ID'     => '1',
            'PARENT' => '0',
            'NAME'   => 'Your first category',
        ),
        1 => array(
            'ID'     => '2',
            'PARENT' => '1',
            'NAME'   => 'Your first forum',
        ),
        2 => array(
            'ID'     => '4',
            'PARENT' => '1',
            'NAME'   => 'Another Forum',
        ),
        3 => array(
            'ID'     => '5',
            'PARENT' => '1',
            'NAME'   => 'Good Forum',
        ),
        4 => array(
            'ID'     => '6',
            'PARENT' => '0',
            'NAME'   => 'Top Forum',
        ),
        5 => array(
            'ID'     => '7',
            'PARENT' => '6',
            'NAME'   => 'Sub Forum #1',
        ),
        6 => array(
            'ID'     => '9',
            'PARENT' => '7',
            'NAME'   => 'Sub Forum #1-1',
        ),
        7 => array(
            'ID'     => '10',
            'PARENT' => '7',
            'NAME'   => 'Sub Forum #1-2',
        ),
        8 => array(
            'ID'     => '8',
            'PARENT' => '6',
            'NAME'   => 'Sub Forum #2',
        ),
    );
    
    // reorder array
    $menu = array();
    foreach ( $data as $item ) {
        $menu[$item['PARENT']][] = $item;
    }
    
    // print menu
    function makeMenu($menu, $parentID) {
        $html = "<ul>";
        foreach ( $menu[$parentID] as $item ) {
            $html .= "<li id='{$item['ID']}'>{$item['NAME']}";
            if ( isset($menu[$item['ID']]) ) {
                $html .= makeMenu($menu, $item['ID']);
            }
            $html .= "</li>";
        }
        $html .= "</ul>";
        return $html;
    }
    echo makeMenu($menu, 0);
    

    输出(手动添加间距):

    <ul>
        <li id='1'>
            Your first category
            <ul>
                <li id='2'>Your first forum</li>
                <li id='4'>Another Forum</li>
                <li id='5'>Good Forum</li>
            </ul>
        </li>
        <li id='6'>
            Top Forum
            <ul>
                <li id='7'>Sub Forum #1
                    <ul>
                        <li id='9'>Sub Forum #1-1</li>
                        <li id='10'>Sub Forum #1-2</li>
                    </ul>
                </li>
                <li id='8'>Sub Forum #2</li>
            </ul>
        </li>
    </ul>
    

答案 1 :(得分:1)

$new_arr = array();
foreach($arr as $data) $new_arr[$data['id']] = $data;

foreach($new_arr as $id => &$data) $data['parent'] =& $new_arr[$data['parent']];

现在你有一个合适的数组结构。

但是你可能不得不把它翻过来,因为它是由孩子构建的。但你的输出是由父母提供的。

因此,不是我发布的第二个foreach,而是执行此操作:

foreach($new_arr as $id => $data) $new_arr[$data['parent']]['children'][] =& $data;