从多维无限父子树创建菜单 [英] Create menu from multi dimensional infinite parent child tree
问题描述
有人吗?
在此先感谢,让我知道如果你需要任何额外的信息。
我试图把一个多维数组多个嵌套的HTML导航菜单。我有它从SO另一个答案JIST:<一href=\"http://stackoverflow.com/questions/8685825/php-loop-through-multidimensional-array-and-establish-parent-child-relationship\">solution这里。
我所想弄清楚是怎么preserve在url中的下一子阵列顶层父链接(及任何后续子页面的链接)。我试图传递的链接时,它调用自身打造阵列的功能,但只有preserved最新的父链接。
例如:
首页
关于结果
-info结果
--sub页
变成了:
家
关于
关于/信息
关于/信息/子页面
下面是一个简单的数组:
阵列
(
[0] =&GT; stdClass的对象
(
[ID] =&GT; 12
[家长] =&GT; 11
[名] =&GT;子页
[链接] =&GT; SUB_PAGE
[目标] =&GT; _自
)[1] =&GT; stdClass的对象
(
[ID] =&GT; 14
[家长] =&GT; 12
[名] =&GT;测试
[链接] =&GT; test_test
[目标] =&GT; _自
)[2] =&GT; stdClass的对象
(
[ID] =&GT; 9
[家长] =&GT; 0
[名] =&GT;家
[链接] =&GT;家
[目标] =&GT; _自
)[3] =&GT; stdClass的对象
(
[ID] =&GT; 11
[家长] =&GT; 10
[名] =&GT;信息
[链接] =&GT;信息
[目标] =&GT; _自
)[4] =&GT; stdClass的对象
(
[ID] =&GT; 13
[家长] =&GT; 10
[名] =&GT;测试
[链接] =&GT;测试
[目标] =&GT; _自
)[5] =&GT; stdClass的对象
(
[ID] =&GT; 10
[家长] =&GT; 0
[名] =&GT;关于
[链接] =&GT;关于
[目标] =&GT; _自
)
)
这里是code我使用的:
函数create_menu_array($改编,$父= 0){
$页=阵列();
的foreach($改编为$页){
如果($页面级&GT;母公司== $父){
$页面级&GT;分=使用isset($页面级&GT;分)? $页面级&GT;分函数:$ this-&GT; create_menu_array($改编,$页面级&GT; ID);
$页[] = $页;
}
}
返回$页;
}功能create_menu_html($ NAV){
$的HTML ='';
的foreach($资产净值为$页){
$ HTML ='&LT; UL&GT;&LT;立GT;';
$的HTML ='&LT; A HREF =。BASE_URL()$页面级方式&gt;。链接'的目标='$页面级方式&gt;的目标。'&GT;' 。 $页面级&GT;的名字。 '&所述; / A&GT;';
$的HTML = $这个 - 方式&gt; create_menu_html($页面级&GT;分);
$ HTML ='&LT; /李&GT;&LT; / UL&GT;';
}
返回$ HTML;
}
通过递归函数解决方法:
例如: http://phpfiddle.org/main/$c$c/ PMW-7i3
&LT; PHP功能array_process_for_ids($项目){
$ new_array =阵列();
的foreach($项目为$项目){
$ new_array [$项目['身份证'] = $项目;
}
返回$ new_array;
}功能菜单($项目){
功能menu_recursive($ parent_item){
全球$物品;
未设置($项目[$ parent_item ['身份证']);
呼应'&LT; DIV的风格=填充左:均为15px;&GT;';
回声 - $ parent_item ['名']。
的foreach($项目为$项目){
如果($项目['父'] == $ parent_item ['身份证']){
menu_recursive($项目);
}
}
呼应'&LT; / DIV&GT;';
}
的foreach($项目为$项目){
如果($项目['父'] == 0)menu_recursive($项目);
}
}$项目=阵列(
阵列(
'ID'=&GT; 10,
父= GT; 0,
'名'=&GT; 首页1',
'链接'=&GT; top_page_1',
目标= GT; '_自'
)
阵列(
'ID'=&GT; 12,
父= GT; 0,
'名'=&GT; 首页2',
'链接'=&GT; top_page_2',
目标= GT; '_自'
)
阵列(
'ID'=&GT; 25,
父= GT; 10,
'名'=&GT; 子页1首页1',
'链接'=&GT; sub_page_1_of_top_page_1',
目标= GT; '_自'
)
阵列(
'ID'=&GT; 26,
父= GT; 12,
'名'=&GT; 子页1 2页面顶部,
'链接'=&GT; sub_page_1_of_top_page_2',
目标= GT; '_自'
)
阵列(
'ID'=&GT; 28,
父= GT; 26,
'名'=&GT; 子页面1 2页面顶部的子页面',
'链接'=&GT; sub_page_of_sub_page_1_of_top_page_2',
目标= GT; '_自'
)
);$项目= array_process_for_ids($项目);菜单($项目);?&GT;
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.
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.
Example:
Home
About
-Info
--Sub Page
becomes:
home about about/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;
}
Solution with recursive function:
example: 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);
?>
这篇关于从多维无限父子树创建菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!