从PHP平板阵列创建一个嵌套的UL [英] Creating a nested UL from flat array in PHP
问题描述
这是一个从JSON文件建立了一个数组。我想要做的就是创建一个无序的嵌套列表。我已经看到了很多的教程在那里,但他们通常只为一个简单的(ID,父ID,名称)的布局工作。该数组比这更复杂这就是为什么我似乎尝试不会工作。
这是期望的结果:
- 标准一:课程,规划和评估
- 指示I-A。课程与放大器;规划
- I-A-1。儿童及青少年发展
- 内容将何去何从在
- I-A-2。儿童及青少年发展
- 内容将何去何从在
- I-A-1。儿童及青少年发展
- 在这里更多的指标都与标准I
- 指示I-A。课程与放大器;规划
- 标准二:......
有多个家长,他们的ID是由 * _ ID
字段分开。我包括重复的领域,具有不同的名称,以允许基于关我看到在线的例子进行比较,可以做类似 $的parentID == $ ID
。我一直在寻找这个转换为树阵,使阅读更容易的方法,但遇到了类似的并发症也有。
因此,要了解下面的结构,这里是一个关键的:
[top_id]
= [标准]
的ID和相同 [顶]
进行比较的原因。
[PARENT_ID]
= [指标]
的ID和相同 [家长]
进行比较的原因。
[child_id]
= [元]
的ID和相同 [家长]
进行比较的原因。
其他则是相关的[元]的内容,我可以展现出来,一旦让我的列表创建成功。
阵列
(
[0] =>排列
(
[top_id] => 1
[顶] => 1
[PARENT_ID] => 2
[家长] => 2
[child_id] =>五
[儿童] =>五
[标准] =>标准一:课程,规划和评估
[指标] =>指示I-A。课程与放大器;规划
[元] =>我-A-1。儿童及青少年发展
[连接] =>这里的一些内容
[有效实践] =>这里的一些内容
[精通] =>这里的一些内容
[建议文物] =>这里的一些内容
) [1] =>排列
(
[top_id] => 1
[顶] => 1
[PARENT_ID] => 2
[家长] => 2
[child_id] => 6
[儿童] => 6
[标准] =>标准一:课程,规划和评估
[指标] =>指示I-A。课程与放大器;规划
[元] => I-A-2。儿童及青少年发展
[连接] =>这里的一些内容
[有效实践] =>这里的一些内容
[精通] =>这里的一些内容
[建议文物] =>这里的一些内容
) )
- 与ATTEMPT示例更新 -
的foreach($为$节点节点= GT; $ V){ // ID的
$ topID = $ V ['top_id'];
$ =的parentID $ V ['PARENT_ID'];
$ childID的= $ V ['child_id'];
$顶部= $ V ['顶'];
$父= $ V ['父'];
$孩子= $ V ['孩子']; //名称值
$标准= $ V ['标准'];
$指标= $ V ['指示器'];
$元素= $ V ['元素'];
$连接= $ V ['连接'];
$实践= $ V ['的有效实践'];
$精通= $ V ['精通'];
$文物= $ V ['建议文物']; 回声< UL>中; 的foreach($标准为$值){
回声<立gt;中;
打印$价值;
回声< UL>中; 的foreach($指标,因为V $){
回声<立gt;中;
打印$ V;
回声< /李>中;
}
回声< / UL>中;
回声< /李>中;
}
呼应'< / UL>';
}
此外
如果($节点[$顶] [] == $ topID []){
回声<立gt;中;
打印$标准;
回声< UL>中;
如果($节点[$父] [] == $的parentID []){
回声<立gt;中;
打印$指标;
回声< /李>中;
}
回声< / UL>中;
回声< /李>中;
}
我会收集数据,并先构造一棵树,然后打印出树。一些示例code:
的foreach($节点作为$项目){
//采集数据通过ID索引的assoc命令阵列
如果(!使用isset($ data_by_id [$项目['top_id'])){
$ data_by_id [$项目['top_id'] =阵列('名'=> $项目['标准']);
}
如果(!使用isset($ data_by_id [$项目['PARENT_ID'])){
$ data_by_id [$项目['PARENT_ID'] =阵列('名'=> $项目['指示']);
}
如果(!使用isset($ data_by_id [$项目['child_id'])){
$ data_by_id [$项目['child_id'] =阵列(
'名'=> $项目['元素'],
'内容'=>阵列(
$项目['连接'],
$项目[的有效实践'],
$项目['精通'],
$项目[建议文物'])
);
}
//构建树 - 我做了一个简单的三层阵列
$树[$项目['top_id'] [$项目['PARENT_ID'] [$项目['child_id']] ++;
}//经过树和打印信息
//这是可以在任意深度的树中使用的递归函数
功能print_tree($数据,$ ARR){
回声< UL> \\ N的;
//树是一个关联数组,其中$关键是节点的ID,
//和$值是子节点或一个整数的阵列。
的foreach($改编为$关键=> $值){
回声<立GT; 。 $数据[$关键] ['名']。 < /李> \\ N的;
如果(使用isset($数据[$关键] ['内容'])及和放大器; is_array($数据[$关键] ['内容'])){
呼应'< UL>';
的foreach($数据[$关键] ['内容']为$叶){
呼应'<立GT;' 。 $叶。 < /李> \\ N的;
}
回声< / UL> \\ N的;
}
//如果$值是一个数组,它是一组子节点 - 即。另一棵树。
//我们可以在树传递给我们的print_tree功能。
如果(is_array($值)){
print_tree($数据,$值);
}
}
回声< / UL> \\ N的;
}print_tree($ data_by_id,$树);
您需要在错误检查添加,奇怪的字符,去除多余的空白,等轰击。
This is an array that was built from a JSON file. What I want to do is create a unordered nested list. I have seen a lot of tutorials out there but they usually only work for a simple (id, parent id, name) layout. This array is more complicated than that which is why my attempts don't seem to work.
This is the desired outcome:
- Standard I: Curriculum, Planning, and Assessment
- Indicator I-A. Curriculum & Planning
- I-A-1. Child and Adolescent Development
- content will go in here
- I-A-2. Child and Adolescent Development
- content will go in here
- I-A-1. Child and Adolescent Development
- More indicators here that are related to Standard I
- Indicator I-A. Curriculum & Planning
- Standard II: ....
There are multiple parents, and their IDs are separated by the *_id
field. I included duplicate fields, with different names, to allow a comparison based off the examples I saw online that could do something like $parentID == $id
. I was looking into ways of converting this to a tree array to make reading it easier, but ran into similar complications there too.
So to understand the structure below, here is a key:
[top_id]
= [standard]
's ID and is the same as [top]
for comparison reasons
[parent_id]
= [indicators]
's ID and is the same as [parent]
for comparison reasons
[child_id]
= [element]
's ID and is the same as [parent]
for comparison reasons
The others are content associated to the [element] which I can get to show up once I get my list created successfully.
Array
(
[0] => Array
(
[top_id] => 1
[top] => 1
[parent_id] => 2
[parent] => 2
[child_id] => 5
[child] => 5
[standard] => Standard I: Curriculum, Planning, and Assessment
[indicator] => Indicator I-A. Curriculum & Planning
[element] => I-A-1. Child and Adolescent Development
[Connections] => some content here
[Effective Practice] => some content here
[Proficient] => some content here
[Suggested Artifacts] => some content here
)
[1] => Array
(
[top_id] => 1
[top] => 1
[parent_id] => 2
[parent] => 2
[child_id] => 6
[child] => 6
[standard] => Standard I: Curriculum, Planning, and Assessment
[indicator] => Indicator I-A. Curriculum & Planning
[element] => I-A-2. Child and Adolescent Development
[Connections] => some content here
[Effective Practice] => some content here
[Proficient] => some content here
[Suggested Artifacts] => some content here
)
)
-- UPDATE WITH ATTEMPT EXAMPLES --
foreach ($nodes as $node => $v) {
// id's
$topID = $v['top_id'];
$parentID = $v['parent_id'];
$childID = $v['child_id'];
$top = $v['top'];
$parent = $v['parent'];
$child = $v['child'];;
// name values
$standard = $v['standard'];
$indicator= $v['indicator'];
$element = $v['element'];
$connections = $v['Connections'];
$practice = $v['Effective Practice'];
$proficient = $v['Proficient'];
$artifacts = $v['Suggested Artifacts'];
echo "<ul>";
foreach($standard as $value){
echo "<li>";
print $value;
echo "<ul>";
foreach($indicator as $v){
echo "<li>";
print $v;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
echo '</ul>';
}
Also
if ($node[$top][] == $topID[]){
echo "<li>";
print $standard;
echo "<ul>";
if ($node[$parent][] == $parentID[]){
echo "<li>";
print $indicator;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
I would gather the data and construct a tree first, and then print the tree out. Some sample code:
foreach ($nodes as $item) {
// gather the data in an assoc array indexed by id
if ( !isset($data_by_id[ $item['top_id'] ])) {
$data_by_id[ $item['top_id'] ] = array( 'name' => $item['standard'] );
}
if ( !isset($data_by_id[ $item['parent_id'] ])) {
$data_by_id[ $item['parent_id'] ] = array( 'name' => $item['indicator'] );
}
if ( !isset($data_by_id[ $item['child_id'] ])) {
$data_by_id[ $item['child_id'] ] = array(
'name' => $item['element'],
'contents' => array(
$item['Connections'],
$item['Effective Practice'],
$item['Proficient'],
$item['Suggested Artifacts'])
);
}
// construct the tree - I've made a simple three tier array
$tree[ $item['top_id'] ][ $item['parent_id'] ][ $item['child_id'] ]++;
}
// go through the tree and print the info
// this is a recursive function that can be used on arbitrarily deep trees
function print_tree( $data, $arr ){
echo "<ul>\n";
// the tree is an associative array where $key is the ID of the node,
// and $value is either an array of child nodes or an integer.
foreach ($arr as $key => $value) {
echo "<li>" . $data[$key]['name'] . "</li>\n";
if (isset($data[$key]['contents']) && is_array($data[$key]['contents'])) {
echo '<ul>';
foreach ($data[$key]['contents'] as $leaf) {
echo '<li>' . $leaf . "</li>\n";
}
echo "</ul>\n";
}
// if $value is an array, it is a set of child nodes--i.e. another tree.
// we can pass that tree to our print_tree function.
if (is_array($value)) {
print_tree($data, $value);
}
}
echo "</ul>\n";
}
print_tree($data_by_id, $tree);
You'll need to add in error checking, zapping of strange characters, removal of excess whitespace, etc.
这篇关于从PHP平板阵列创建一个嵌套的UL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!