以下是我的http://codepad.org/iDoXXsLX
有这样的数组
Array
(
[0] => Array
(
[NumberRenamed] => 17
[TopicName] => Products
[UpperLevelNumberRenamed] => 0
)
[17] => Array
(
[0] => Array
(
[1] => Array
(
[NumberRenamed] => 18
[TopicName] => Computers
[UpperLevelNumberRenamed] => 17
)
)
)
[18] => Array
(
[0] => Array
(
[2] => Array
(
[NumberRenamed] => 16
[TopicName] => Laptops
[UpperLevelNumberRenamed] => 18
)
)
)
[16] => Array
(
[0] => Array
(
[4] => Array
(
[NumberRenamed] => 8
[TopicName] => Dell
[UpperLevelNumberRenamed] => 16
)
)
)
)
顶级项目为Products
,第一个子级别项目为Computers
,下一个子级别为Laptops
,然后是下一个子级别Dell
对于每个子级别项UpperLevelNumberRenamed
==最接近的上级NumberRenamed
。
想要得到这样的结果
Products
Computers
Laptops
Dell
Acer
Desktops
Home
试过这个
foreach( $main_topics as $k_main_topics => $v_main_topics ){
if( isset($v_main_topics['UpperLevelNumberRenamed']) and $v_main_topics['UpperLevelNumberRenamed'] == 0 ){
//print only top level topics
echo $v_main_topics['TopicName']. '<br/>';
}
else{//if not top level topic
foreach( $v_main_topics[0] as $k_v_main_topics_0 => $v_v_main_topics_0 ){
echo $v_v_main_topics_0['TopicName']. '<br/>';
}//foreach( $v_main_topics[0] as $k_v_main_topics_0 => $v_v_main_topics_0 )
}//else{
}//foreach( $main_topics as $k_main_topics => $v_main_topics )
但是得到这个
Products
Home
Computers
Laptops
Desktops
Dell
Acer
有些不对劲,但无法理解是什么。请建议在代码中纠正/更改的内容
尝试其他方式
初始数组是一维数组。试图从一维获得ul li导航。
以下是我所做的http://codepad.org/OLtxyL4X
答案 0 :(得分:4)
以下是它的作用摘要:
这是:
$flat = array();
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($main_topics)) as $i)
$flat[] = $i;
$final = array();
$defs = array();
for ($i = 0; $i < count($flat); $i += 3)
if ($flat[$i + 2] == 0) {
$final[$flat[$i + 1]] = array();
$defs[$flat[$i]] = &$final[$flat[$i + 1]];
} else {
$defs[$flat[$i + 2]][$flat[$i + 1]] = array();
$defs[$flat[$i]] = &$defs[$flat[$i + 2]][$flat[$i + 1]];
}
function array2ul($array) {
$out = "<ul>";
foreach($array as $key => $elem)
$out = is_array($elem) ?
$out . "<li><span>$key</span>" . array2ul($elem) . "</li>" :
$out = $out."<li><span>$key:[$elem]</span></li>";
$out = $out . "</ul>";
return $out;
}
echo array2ul($final);
<ul><li><span>Products</span><ul><li><span>Computers</span><ul><li><span>Laptops</span><ul><li><span>Dell</span><ul></ul></li><li><span>Acer</span><ul></ul></li></ul></li><li><span>Desktops</span><ul></ul></li></ul></li></ul></li><li><span>Home</span><ul></ul></li></ul>
答案 1 :(得分:0)
这应该是一个使用递归的工作示例,但未经过测试:
定义数组
colnames(df2)[-1] <- LETTERS[1:4]
row.names(df2) <- NULL
df2
MonYear A B C D
1 01-01-15 28.21 1333.00 10.05 18.22
2 01-02-15 42.03 1348.64 5.46 18.41
3 01-03-15 48.56 1364.28 4.82 14.31
4 01-04-15 46.85 1379.92 5.27 30.28
5 01-05-15 46.03 1395.56 5.07 18.16
6 01-06-15 54.60 1411.20 4.07 15.52
7 01-07-15 63.87 1426.84 9.53 12.52
8 01-08-15 50.00 1442.48 1.95 13.14
9 01-09-15 53.34 1458.11 6.95 15.05
10 01-10-15 43.47 1473.75 6.54 8.89
11 01-11-15 34.66 1489.39 5.91 12.51
12 01-12-15 27.48 1505.03 0.56 25.25
创建并调用函数:
$main_array = Array
(
'10' => Array
(
'name' => 'Products'
'children' => Array
(
'12' => Array
(
'name' => 'Laptop',
'children' => Array
(
'13' => Array
(
'name' => 'Dell',
),
'14' => Array
(
'name' => 'Acer',
)
)
)
'14' => Array
(
'name' => 'Desktop',
'children' => Array
(
'15' => Array
(
'name' => 'Sony',
),
'16' => Array
(
'name' => 'Apple',
)
)
),
)
)
)