我一直在尝试用PHP创建一个多级有序列表。我已经尝试过并且一直在搜索,我看到的所有内容都有不同的数据布局。大多数示例从数据库中提取数据并基于单独的列构建数组,并且在具体情况下,是具有深度或父母关系信息的列。我的数据位于SSRS报告的路径中,可能会有2到3个(或将来更多),所以我不确定如何构建数组以使用此问题中的函数。
例如,一个例子来自这里的另一篇文章 - generating-category-tree-as-html-unordered-list,但所有例子和我所拥有的不同之处在于数据本身的性质。
以下是我正在使用的完整示例:
示例数组和函数
<?php
$nested = Array
(
1 => Array ('id' => 1, 'parent' => 0, 'title' => 'Page 1'),
2 => Array ('id' => 2, 'parent' => 0, 'title' => 'Page 2'),
3 => Array ('id' => 3, 'parent' => 0, 'title' => 'Page 3'),
4 => Array ('id' => 4, 'parent' => 0, 'title' => 'Page 4' ),
5 => Array ('id' => 5, 'parent' => 0, 'title' => 'Page 5'),
6 => Array ('id' => 6, 'parent' => 1, 'title' => 'Page 1-1'),
7 => Array ('id' => 7, 'parent' => 1, 'title' => 'Page 1-2'),
8 => Array ('id' => 8, 'parent' => 1, 'title' => 'Page 1-3'),
9 => Array ('id' => 9, 'parent' => 2, 'title' => 'Page 2-1'),
10 => Array ('id' => 10, 'parent' => 2, 'title' => 'Page 2-2'),
11 => Array ('id' => 11, 'parent' => 2, 'title' => 'Page 2-3'),
12 => Array ('id' => 12, 'parent' => 3, 'title' => 'Page 3-1'),
13 => Array ('id' => 13, 'parent' => 3, 'title' => 'Page 3-2'),
14 => Array ('id' => 14, 'parent' => 4, 'title' => 'Page 4-1'),
15 => Array ('id' => 15, 'parent' => 6, 'title' => 'Page 1-1-1'),
16 => Array ('id' => 16, 'parent' => 6, 'title' => 'Page 1-1-2'),
17 => Array ('id' => 17, 'parent' => 6, 'title' => 'Page 1-1-3'),
18 => Array ('id' => 18, 'parent' => 7, 'title' => 'Page 1-2-1'),
19 => Array ('id' => 19, 'parent' => 7, 'title' => 'Page 1-2-2'),
20 => Array ('id' => 20, 'parent' => 7, 'title' => 'Page 1-2-3'),
21 => Array ('id' => 21, 'parent' => 9, 'title' => 'Page 2-1-1'),
22 => Array ('id' => 22, 'parent' => 9, 'title' => 'Page 2-1-2'),
23 => Array('id' => 23, 'parent' => 12, 'title' => 'Page 2-3-3')
);
function recursive($parent, $array) {
$has_children = false;
foreach($array as $key => $value) {
if ($value['parent'] == $parent) {
if ($has_children === false && $parent) {
$has_children = true;
echo '<ul>' ."\n";
}
echo '<li>' . "\n";
echo '<a href="/page.php?id=' . $value['id'] . '">' . $value['title'] . '</a>' . " \n";
echo "\n";
recursive($key, $array);
echo "</li>\n";
}
}
if ($has_children === true && $parent) echo "</ul>\n";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<ul><?php echo recursive(0, $nested); ?></ul>
</body>
</html>
以下是SSRS报告服务器的数据示例。由于数据非常密集,它不适合代码窗口并且没有任何意义,因此我将不得不堆叠两列。报告名称与路径中的最后一个元素相同,因此无论如何它都有点多余。
数据库数据
PATH
/Production/Analyst/POS Sales Excel Data Export
/Production/Analyst/STATS Membership Tracking - RAW
/Production/Information Technologies/IT Dashboard DUMMY
/Production/Information Technologies/IT Department
/Production/Membership/MDash
/Production/Membership/Membership Dashboard
/Production/Membership/Membership Reports/Membership Grid
/Production/Membership/Membership Reports/Membership Grid by SU
/Production/Membership/Membership Reports/Membership Pie Charts
/Production/Membership/Membership Reports/Membership Sum Drill Down
/Production/Membership/Membership Reports/POS Sales By Date and Location
/Production/Membership/Stats/STATS Council Participation
/Production/Membership/Stats/STATS Council SU Summary Adults
/Production/Membership/Stats/STATS Council Summary Adults
/Production/Membership/Stats/STATS Council Troop Summary Adults
/Production/Membership/Stats/STATS Lifetime Stats
/Production/Membership/Stats/STATS Membership Tracking
/Production/Online Training (LMS)/LMS User ID Lookup
/Production/Online Training (LMS)/Manager Training 2014
/Production/Shop/POS Reports/POS Sales By Date and Location
/Production/Shop/Shop Dashboard DUMMY
/Production/SLT Dashboard/OLD Dashboard Membership_Overview
NAME
POS Sales Excel Data Export
STATS Membership Tracking - RAW
IT Dashboard DUMMY
IT Department
MDash
Membership Dashboard
Membership Grid
Membership Grid by SU
Membership Pie Charts
Membership Sum Drill Down
POS Sales By Date and Location
STATS Council Participation
STATS Council SU Summary Adults
STATS Council Summary Adults
STATS Council Troop Summary Adults
STATS Lifetime Stats
STATS Membership Tracking
LMS User ID Lookup
Manager Training 2014
POS Sales By Date and Location
Shop Dashboard DUMMY
OLD Dashboard Membership_Overview
该数据需要采用如下格式才能使用该功能。 ID与我的目的无关,因为报告的链接与报告名称相同,因此我必须更改功能以适应。也就是说,我需要采取上面的路径并将其转换为如下所示的数组。
需要数组格式
$nested2 = Array
(
1 => Array ('parent' => 0, 'title' => 'Analyst'),
2 => Array ('parent' => 0, 'title' => 'Membership'),
3 => Array ('parent' => 0, 'title' => 'SLT Dashboard'),
4 => Array ('parent' => '1', 'title' => 'POS Sales Excel Data Export'),
5 => Array ('parent' => '1', 'title' => 'STATS Membership Tracking - RAW'),
6 => Array ('parent' => '2', 'title' => 'Membership Dashboard'),
7 => Array ('parent' => '6', 'title' => 'Membership Grid'),
8 => Array ('parent' => '6', 'title' => 'Membership Grid by Service Unit'),
);
所以,我的问题是 - 如何将我正在使用的数据转换为这样的数组?或者,使用不同的东西创建列表会更好吗?我知道我可以遍历列表,可能需要将每个元素与报告名称进行比较,以了解停止的位置,父级是谁,等等,但在此之后会丢失。
一如既往,非常感谢。
答案 0 :(得分:0)
我写了一个简单的解决方案(希望,这对你有用,你可以修改你想要的方式)。但我只使用了PATH
部分(如何处理部分NAME
- 不明白)。
$filename = 'path.txt'; // put section PATH to file for convenience
$lines = file($filename);
$tree = array();
foreach($lines as $line){
$items = array_filter(explode('/',$line));
$branch = array();
$ptr = &$branch;
foreach($items as $item){
$ptr[$item] = Array();
$ptr = &$ptr[$item];
}
unset($ptr);
if (empty($tree)){
$tree = $branch;
}else{
$tree = array_merge_recursive($tree,$branch);
}
}
// output tree
echo '<pre>';
var_dump($tree);
echo '</pre>';
<强>输出强>
array(1) {
["Production"]=>
array(6) {
["Analyst"]=>
array(2) {
["POS Sales Excel Data Export"]=> array(0) {}
["STATS Membership Tracking - RAW"]=> array(0) {}
}
["Information Technologies"]=>
array(2) {
["IT Dashboard DUMMY"]=> array(0) {}
["IT Department"]=> array(0) {}
}
["Membership"]=>
array(4) {
["MDash"]=>array(0) {}
["Membership Dashboard"]=> array(0) {}
["Membership Reports"]=>
array(5) {
["Membership Grid"]=> array(0) {}
["Membership Grid by SU"]=> array(0) {}
["Membership Pie Charts"]=> array(0) {}
["Membership Sum Drill Down"]=> array(0) {}
["POS Sales By Date and Location"]=> array(0) {}
}
["Stats"]=>
array(6) {
["STATS Council Participation"]=> array(0) {}
["STATS Council SU Summary Adults"]=> array(0) {}
["STATS Council Summary Adults"]=> array(0) {}
["STATS Council Troop Summary Adults"]=>array(0) {}
["STATS Lifetime Stats"]=> array(0) {}
["STATS Membership Tracking"]=> array(0) {}
}
}
["Online Training (LMS)"]=>
array(2) {
["LMS User ID Lookup"]=> array(0) {}
["Manager Training 2014"]=> array(0) {}
}
["Shop"]=>
array(2) {
["POS Reports"]=>
array(1) {
["POS Sales By Date and Location"]=> array(0) {}
}
["Shop Dashboard DUMMY"]=> array(0) {}
}
["SLT Dashboard"]=>
array(1) {
["OLD Dashboard Membership_Overview"]=> array(0) {}
}
}
}