[英]Creating a nested UL from flat array in PHP
這是從JSON文件構建的數組。 我想做的是創建一個無序的嵌套列表。 我在那里看到了很多教程,但它們通常僅適用於簡單的(標識,父標識,名稱)布局。 這個數組比之復雜,這就是為什么我的嘗試似乎不起作用的原因。
這是理想的結果:
有多個父級,並且其ID由*_id
字段分隔。 我包括了重復的字段,使用不同的名稱,以允許根據我在網上看到的示例進行比較,這些示例可以執行類似$parentID == $id
。 我一直在研究將其轉換為樹形數組的方法,以使其閱讀起來更容易,但在那里也遇到了類似的麻煩。
因此,要了解下面的結構,這是一個關鍵:
[top_id]
= [standard]
的ID,並且出於比較原因與[top]
相同
[parent_id]
= [indicators]
的ID,出於比較原因與[parent]
相同
[child_id]
= [element]
的ID,出於比較原因與[parent]
相同
其他是與[element]相關的內容,一旦成功創建列表,我就可以顯示該內容。
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
)
)
-嘗試示例更新-
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>';
}
也
if ($node[$top][] == $topID[]){
echo "<li>";
print $standard;
echo "<ul>";
if ($node[$parent][] == $parentID[]){
echo "<li>";
print $indicator;
echo "</li>";
}
echo "</ul>";
echo "</li>";
}
我會先收集數據並構造一棵樹,然后將其打印出來。 一些示例代碼:
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);
您需要添加錯誤檢查,更改奇怪字符,清除多余的空格等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.