我很难想出来,我会感激任何帮助!
我想通过在标签中放置标签来将一个字符串循环到它自己的子菜单中
$string = 'foler1/folder2/folder3/folder4/';
$array = explode('/', substr_replace($string, '', -1));
foreach($array as $each_array){ // do something echo "<li>
<span>$each_array</span>
<ul>
//add new sub
</ul>
</li>";}
我想要这个输出
//I would like to loop out this html code with each name
<li>
<span>folder1</span>
<ul>
<li>
<span>folder2</span>
<ul>
<li>
<span>folder3</span>
<ul>
<li>
<span>folder4</span>
<ul>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
我设法找出了我需要的东西
foreach($array as $each_array){
$list .= "<li>
<span>$each_array</span>
<ul>";}
foreach($array as $each_array){
$list .= "</ul></li>";}
echo $list;
答案 0 :(得分:1)
这不是一个有效的答案,但它有效
<?php
$string = 'foler1/folder2/folder3/folder4/';
$array = explode('/', substr_replace($string, '', -1));
for ($i=0;$i<count($array);$i++)
{
if($i<count($array)-1)
{
echo '<li><span>'.$array[$i].'</span><ul>';
}
else
{
echo '<li><span>'.$array[$i].'</span>';
}
}
for ($i=0;$i<count($array)-1;$i++)
{
echo '</ul></li>';
}
?>