我花了很多时间用这个,但无法生成解决方案,所以我请求你的帮助:) 我有一个这种结构的多维PHP数组:
Array
(
[1] => Array
(
[title] => Dance Type
[id] => 2
[children] => Array
(
[0] => Tango
[1] => Salsa
[2] => Hip-Hop
)
)
[2] => Array
(
[title] => Age
[id] => 3
[children] => Array
(
[0] => Young
[1] => Teen
[2] => Senior
)
)
[3] => Array
(
[title] => Grade
[id] => 1
[children] => Array
(
[0] => Professional
[1] => Student
)
)
[4] => Array
(
[title] => Groups
[id] => 4
[children] => Array
(
[0] => Small Group
[1] => Duet
)
)
)
此数组可能包含无限量的键,不一定是4。
我需要的是生成以下结构的嵌套“计划”(ul / li):
<ul>
<li>Tango
<ul>
<li>Young
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Teen
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Senior
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>Salsa
<ul>
<li>Young
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Teen
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Senior
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>Hip-Hop
<ul>
<li>Young
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Teen
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
<li>Senior
<ul>
<li>Professional
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
<li>Student
<ul>
<li>Small Group</li>
<li>Duet</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
提前感谢您的帮助!
编辑:此数组直接从MySQL数据库生成(使用一个表): eventsRegistrationRoutinesCategories表:
| categoriesID [int(10)] | categoryTitle [text] | optionTitle [text] |
------------------------------------------------------------------------
| 2 | Dance Type | Tango |
------------------------------------------------------------------------
| 3 | Age | Young |
------------------------------------------------------------------------
| 1 | Grade | Professional |
------------------------------------------------------------------------
| 4 | Groups | Small Group |
------------------------------------------------------------------------
| 2 | Dance Type | Salsa |
------------------------------------------------------------------------
| 3 | Age | Teen |
------------------------------------------------------------------------
| 1 | Grade | Student |
------------------------------------------------------------------------
| 4 | Groups | Duet |
------------------------------------------------------------------------
| 2 | Dance Type | Salsa |
------------------------------------------------------------------------
| 3 | Age | Senior |
------------------------------------------------------------------------
| 2 | Dance Type | Hip-Hop |
------------------------------------------------------------------------
etc...
$schedule = array();
$sSQL="SELECT DISTINCT(categoriesID), categoryTitle FROM eventsRegistrationRoutinesCategories WHERE eventsID = '".$eventsID."' ORDER BY eventsRegistrationRoutinesCategoriesID";
$result = mysqli_query($linkMySQL, $sSQL) or sqlError($sSQL,mysqli_error($linkMySQL));
$count1 = 1;
while($row = mysqli_fetch_array($result)) {
$schedule[$count1]['title'] = $row['categoryTitle'];
$schedule[$count1]['id'] = $row['categoriesID'];
// Get all sub options for each
$sSQL2="SELECT * FROM eventsRegistrationRoutinesCategories WHERE categoriesID = '".$row["categoriesID"]."' GROUP BY categoriesOptionsID ORDER BY eventsRegistrationRoutinesCategoriesID";
$result2=mysqli_query($linkMySQL, $sSQL2) or sqlError($sSQL2,mysqli_error($linkMySQL));
while($row2 = mysqli_fetch_array($result2)) {
if(!empty($row2["optionTitle"])) {
$schedule[$count1]['children'][] = $row2["optionTitle"];
}
}
$count1++;
}
答案 0 :(得分:0)
使用此..希望这有帮助
$array=//Your input array
foreach($array as $row) {
$result[$row['title']]=$row['children'];
}
echo '<ul>';
foreach($result['Dance Type'] as $dance_type) {
echo '<li>'.$dance_type.'</li>';
echo '<ul>';
foreach($result['Age'] as $age) {
echo '<li>'.$age.'</li>';
echo '<ul>';
foreach($result['Grade'] as $grade) {
echo '<li>'.$grade.'</li>';
echo '<ul>';
foreach($result['Groups'] as $groups) {
echo '<li>'.$groups.'</li>';
}
echo '</ul>';
}
echo '</ul>';
}
echo '</ul>';
}
echo '</ul>';
答案 1 :(得分:0)
我已经想出了这个并且会发布一个答案,以防有人需要它:) 我已经调用了我们的数组&#34; $ scheduleNested&#34;。然后我创建了一个函数:
function buildTree($array, $depth) {
global $scheduleNested;
echo '<ul>';
foreach($array as $row) {
echo '<li>';
echo $row;
if(isset($scheduleNested[$depth+1])) {
buildTree($scheduleNested[$depth+1]['children'], $depth+1);
}
echo '</li>';
}
echo '</ul>';
}
然后我们在div中调用此函数:
<div class="dd" id="nestable2">
<?
buildTree($scheduleNested[1]['children'],1);
?>
</div>