php - 使用 PHP 生成嵌套 ul 列表的问题

标签 php jquery html data-structures

我正在开发一个前端网络应用程序,其中嵌套的无序列表将用于 jQuery 插件 mcdropdown。

这是来自 PHP 的数据结构:数组的嵌套数组:

Array
(
    [0] => Array
        (
            [fullpath] => ../foil/alphanumeric/
            [depth] => 0
        )

    [1] => Array
        (
            [fullpath] => ../foil/alphanumeric/letters/
            [depth] => 1
        )

    [2] => Array
        (
            [fullpath] => ../foil/alphanumeric/numbers/
            [depth] => 1
        )

    [3] => Array
        (
            [fullpath] => ../foil/alphanumeric/numbers/symbols/
            [depth] => 2
        )
)

基本上,我从 this question on SO 得到了很好的答案,稍微修改了一下:

global $fullpaths; // $fullpaths contains the above data structure in print_r
$result = '';
$currentDepth = -1;

while(!empty($fullpaths))
{
    $currentNode = array_shift($fullpaths);

    if($currentNode['depth'] > $currentDepth)
    {
        $result .='<ul>';
    }

    if($currentNode['depth'] < $currentDepth)
    {
        $result .=str_repeat('</ul>', $currentDepth - $currentNode['depth']);
    }

    $result .= '<li>'. $currentNode['fullpath'] .'</li>';

    $currentDepth = $currentNode['depth'];

    if(empty($fullpaths))
    {
        $result .= str_repeat('</ul>', 1 + $currentDepth);
    }
}

print $result;

得到如下输出:

<ul>
    <li>../foil/alphanumeric/</li>
    <ul>
        <li>../foil/alphanumeric/letters/</li>
        <li>../foil/alphanumeric/numbers/</li>
        <ul>
            <li>../foil/alphanumeric/numbers/symbols/</li>
        </ul>
    </ul>
</ul>

mcdropdown jQuery 插件不能接受它,它需要这样的东西:

<li rel="1">
'Alphanumeric'
    <ul>
        <li rel="2">'Letters'</li>
        <li rel="3">'Numbers'
            <ul>
                <li rel="4">'Symbols'</li>
            </ul>
        </li>
    </ul>
</li>

坦率地说,我不太明白那个问题的答案是如何工作的,我一直在尝试修改那个解决方案来应对我的情况,但仍然失败了。

任何帮助和建议都是事先适当的。

最佳答案

如果你已经有了正确的深度值,那么你就不需要递归了。我有一个类似的函数用于

    -
  • -generation:

    function ulli($newlevel, &$level, $UL="ul", $once=1) {
    
      if ($level == $newlevel) {
         echo "</li>\n";
      }
    
      while ($level<$newlevel) {
         $level++;
         echo "\n  <$UL>\n";
      }
    
      while ($level>$newlevel) {
         if ($once-->0) { echo "</li>\n"; } 
         $level--;
         echo "  </$UL>"
         . ($level>0 ? "</li>" : "") . "\n";  // skip for final </ul> (level=0)
      }
    }
    

    它需要一个当前的 $level 变量作为引用 (=$currentDepth)。然后你将你的深度作为 $newlevel 传递给它。然而,它需要第一个深度为 1。

    基本用法如下:

    $currentDepth=0;
    foreach ($array as $_) {
       ulli($_["depth"]+1, $currentDepth);
       echo "<li>$_[path]";
    }
    ulli(0, $currentDepth);
    

    好吧,古怪。但这对我有用。

关于php - 使用 PHP 生成嵌套 ul 列表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2988290/

相关文章:

javascript - 反向HTML标签,理解赋值

php - PHP shell_exec 中的 FFMPEG 命令对于 300Mo mp4 来说太慢了

php - 获取给定日期的总数量和价格

jquery - 将点击处理程序分配给新附加的元素

jquery - 在 MVC4 中隐藏或显示 DIV

javascript - IE输入不同行为选择文本与否

php - Laravel POST 调用返回 index() 函数而不是 store()

php - 从任何 YouTube URL 获取 YouTube 视频 ID 的正则表达式模式

javascript - 获取当前选择的选项

html - 当一个 li 元素高度增加时,让其他 li 元素环绕