从多维无限父子树创建菜单

时间:2022-04-01 01:45:44

Anyone?

任何人?

Thanks in advance, and let me know if you need any additional info.

在此先感谢,如果您需要任何其他信息,请告诉我们。

I am attempting to turn a multi dimensional array to a multiple nested html navigation menu. I have the jist of it from another answer on SO: solution here.

我试图将多维数组转换为多个嵌套的html导航菜单。我从另一个答案中得到了关于SO的解决方案:解决方案。

What I am trying to figure out is how to preserve the top level parent link (and any subsequent child page links) in the url for the next child array. I tried passing in the link to the function when it calls itself to build the array, but that only preserved the most recent parent link.

我想弄清楚的是如何在下一个子数组的url中保留*父链接(以及任何后续的子页面链接)。当我调用自己来构建数组时,我尝试传递到函数的链接,但这只保留了最新的父链接。

Example:

例:

Home About
-Info
--Sub Page

首页关于-Info - 子页面

becomes:

变为:

home about about/info about/info/subpage

主页关于/ info about / info / subpage

Here is a sample array:

这是一个示例数组:

Array
(
[0] => stdClass Object
    (
        [id] => 12
        [parent] => 11
        [name] => Sub Page
        [link] => sub_page
        [target] => _self
    )

[1] => stdClass Object
    (
        [id] => 14
        [parent] => 12
        [name] => Test
        [link] => test_test
        [target] => _self
    )

[2] => stdClass Object
    (
        [id] => 9
        [parent] => 0
        [name] => Home
        [link] => home
        [target] => _self
    )

[3] => stdClass Object
    (
        [id] => 11
        [parent] => 10
        [name] => Info
        [link] => info
        [target] => _self
    )

[4] => stdClass Object
    (
        [id] => 13
        [parent] => 10
        [name] => Test
        [link] => test
        [target] => _self
    )

[5] => stdClass Object
    (
        [id] => 10
        [parent] => 0
        [name] => About
        [link] => about
        [target] => _self
    )

)

And here is the code I am using:

这是我正在使用的代码:

function create_menu_array($arr, $parent = 0){
    $pages = array();
    foreach($arr as $page){
        if($page->parent == $parent){               
            $page->sub = isset($page->sub) ? $page->sub : $this->create_menu_array($arr, $page->id);
            $pages[] = $page;
        }
    }
    return $pages;
}

function create_menu_html($nav){
    $html = '';
    foreach($nav as $page){
        $html .= '<ul><li>';
        $html .= '<a href="' . base_url().$page->link . '" target="'.$page->target.'">' . $page->name . '</a>';
        $html .= $this->create_menu_html($page->sub);
        $html .= '</li></ul>';
    }
    return $html;
}

1 个解决方案

#1


0  

Solution with recursive function:

具有递归函数的解决方案:

example: http://phpfiddle.org/main/code/pmw-7i3

例如:http://phpfiddle.org/main/code/pmw-7i3

<?php

function array_process_for_ids($items) {
    $new_array = array();
    foreach ($items as $item) {
        $new_array[$item['id']] = $item;
    }
    return $new_array;
}

function menu($items) {
    function menu_recursive($parent_item) {
        global $items;
        unset($items[$parent_item['id']]);
        echo '<div style="padding-left: 15px;">';
        echo '- '.$parent_item['name'];
        foreach ($items as $item) {
            if ($item['parent'] == $parent_item['id']) {
                menu_recursive($item);
            }
        }
        echo  '</div>';
    }
    foreach ($items as $item) {
        if ($item['parent'] == 0) menu_recursive($item);
    }
}

$items = array(
    array(
        'id' => 10,
        'parent' => 0,
        'name' => 'Top Page 1',
        'link' => 'top_page_1',
        'target' => '_self'
    ),
    array(
        'id' => 12,
        'parent' => 0,
        'name' => 'Top Page 2',
        'link' => 'top_page_2',
        'target' => '_self'
    ),
    array(
        'id' => 25,
        'parent' => 10,
        'name' => 'Sub Page 1 of top page 1',
        'link' => 'sub_page_1_of_top_page_1',
        'target' => '_self'
    ),
    array(
        'id' => 26,
        'parent' => 12,
        'name' => 'Sub Page 1 of top page 2',
        'link' => 'sub_page_1_of_top_page_2',
        'target' => '_self'
    ),
    array(
        'id' => 28,
        'parent' => 26,
        'name' => 'Sub Page of sub page 1 of top page 2',
        'link' => 'sub_page_of_sub_page_1_of_top_page_2',
        'target' => '_self'
    )
);

$items = array_process_for_ids($items);

menu($items);

?>

#1


0  

Solution with recursive function:

具有递归函数的解决方案:

example: http://phpfiddle.org/main/code/pmw-7i3

例如:http://phpfiddle.org/main/code/pmw-7i3

<?php

function array_process_for_ids($items) {
    $new_array = array();
    foreach ($items as $item) {
        $new_array[$item['id']] = $item;
    }
    return $new_array;
}

function menu($items) {
    function menu_recursive($parent_item) {
        global $items;
        unset($items[$parent_item['id']]);
        echo '<div style="padding-left: 15px;">';
        echo '- '.$parent_item['name'];
        foreach ($items as $item) {
            if ($item['parent'] == $parent_item['id']) {
                menu_recursive($item);
            }
        }
        echo  '</div>';
    }
    foreach ($items as $item) {
        if ($item['parent'] == 0) menu_recursive($item);
    }
}

$items = array(
    array(
        'id' => 10,
        'parent' => 0,
        'name' => 'Top Page 1',
        'link' => 'top_page_1',
        'target' => '_self'
    ),
    array(
        'id' => 12,
        'parent' => 0,
        'name' => 'Top Page 2',
        'link' => 'top_page_2',
        'target' => '_self'
    ),
    array(
        'id' => 25,
        'parent' => 10,
        'name' => 'Sub Page 1 of top page 1',
        'link' => 'sub_page_1_of_top_page_1',
        'target' => '_self'
    ),
    array(
        'id' => 26,
        'parent' => 12,
        'name' => 'Sub Page 1 of top page 2',
        'link' => 'sub_page_1_of_top_page_2',
        'target' => '_self'
    ),
    array(
        'id' => 28,
        'parent' => 26,
        'name' => 'Sub Page of sub page 1 of top page 2',
        'link' => 'sub_page_of_sub_page_1_of_top_page_2',
        'target' => '_self'
    )
);

$items = array_process_for_ids($items);

menu($items);

?>