Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP生成嵌套ul列表时出现问题_Php_Jquery_Html_Data Structures - Fatal编程技术网

使用PHP生成嵌套ul列表时出现问题

使用PHP生成嵌套ul列表时出现问题,php,jquery,html,data-structures,Php,Jquery,Html,Data Structures,我正在开发一个前端web应用程序,其中一个嵌套的无序列表将用于jQuery插件mcdropdown 以下是PHP中的数据结构:一个嵌套数组: Array ( [0] => Array ( [fullpath] => ../foil/alphanumeric/ [depth] => 0 ) [1] => Array ( [fullpath

我正在开发一个前端web应用程序,其中一个嵌套的无序列表将用于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
        )
)
基本上,我从中得到了一个很好的答案,对它做了一些修改:

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;
global$fullpath;//$完整路径在打印中包含上述数据结构
$result='';
$currentDepth=-1;
而(!empty($fullpath))
{
$currentNode=array\u shift($fullpath);
如果($currentNode['depth']>$currentDepth)
{
$result.='
    '; } 如果($currentNode['depth']<$currentDepth) { $result.=str_repeat(“
”,$currentDepth-$currentNode['depth']); } $result.='
  • 。$currentNode['fullpath'].
  • ; $currentDepth=$currentNode['depth']; if(空($fullpath)) { $result.=str_repeat(“”,1+$currentDepth); } } 打印$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>
    
    • ../foil/字母数字/
      • ../foil/字母数字/字母/
      • ../foil/字母数字/数字/
        • ../foil/字母数字/数字/符号/
    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>
    
  • “字母数字”
    • “字母”
    • “数字”
      • “符号”
  • 坦率地说,我不太明白这个问题的答案是如何起作用的,我一直试图修改这个解决方案来应对我的情况,但还是失败了


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

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

      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)
        }
      }
      
      函数ulli($newlevel,&$level,$UL=“UL”,$one=1){
      如果($level==$newlevel){
      回声“
    • \n”; } while($level$newlevel){ 如果($once-->0){echo“\n”} $level--; 回声“” .($level>0?:”)。“\n”//跳到期末考试(level=0) } } 它需要一个当前的$level变量作为参考($currentDepth)。您将其作为$newlevel传递给您的深度。但是,它需要第一个深度为1

      基本用法如下:

      $currentDepth=0;
      foreach ($array as $_) {
         ulli($_["depth"]+1, $currentDepth);
         echo "<li>$_[path]";
      }
      ulli(0, $currentDepth);
      
      $currentDepth=0;
      foreach($数组为$){
      ulli($[“深度”]+1,$currentDepth);
      回音“
    • $\[path]”; } ulli(0,$currentDepth);
    • 嗯,古怪。但是它对我很有用。

      这个代码(缩进分开)会产生你想要的结果吗

      $d = 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
          )
        );
      
      echo "<ul>\n";
      $cdepth = 0; $rel = 1; $first = true; $veryfirst = true;
      foreach($d as $e)
      {
        $mpath = "'" . ucfirst(basename($e['fullpath'])) ."'";
        if ( $e['depth'] == $cdepth ) {
          if ( $first && !$veryfirst) { echo "</li>\n";}
          echo "<li rel=\"$rel\">", $mpath;
          $rel++; $first = false; $veryfirst = false;
        } else {
          $depthdiff = $e['depth'] - $cdepth;
          if ( $depthdiff < 0 ) {
            for($i = 0; $i < -$depthdiff; $i++) {
          echo "</ul>\n</li>\n";
            }
          } else {
            for($i = 0; $i < $depthdiff; $i++) {
          echo "\n<ul>\n";
          $first = true;
              // indeed buggy if $depthdiff > 1...
            }
          }
          echo "<li rel=\"$rel\">", $mpath, "\n";
          $rel++; $first = true;
        }
        $cdepth = $e['depth'];
      }
      for($i = 0; $i < $cdepth; $i++) {
        echo "</ul>\n</li>\n";
      }
      echo "</ul>\n";
      
      $d=array(
      0=>数组(
      “完整路径”=>“../foil/字母数字/”,
      “深度”=>0
      ),
      1=>数组(
      “完整路径”=>“../foil/字母数字/字母/”,
      “深度”=>1
      ),
      2=>数组(
      “完整路径”=>“../foil/字母数字/数字/”,
      “深度”=>1
      ),
      3=>数组(
      “完整路径”=>“../foil/字母数字/数字/符号/”,
      “深度”=>2
      )
      );
      回声“
        \n”; $cdepth=0$rel=1$第一个=正确$veryfirst=true; 外汇兑换($d为$e) { $mpath=“””.ucfirst(basename($e['fullpath']))。“”; 如果($e['depth']=$cdepth){ 如果($first&!$veryfirst){echo“\n”} 回声“
      • ”,$mpath; $rel++;$first=false;$veryfirst=false; }否则{ $depthdiff=$e['depth']-$cdepth; 如果($depthdiff<0){ 对于($i=0;$i<-$depthdiff;$i++){ 回声“
      \n\n”; } }否则{ 对于($i=0;$i<$depthdiff;$i++){ 回声“\n
        \n”; $first=true; //如果$depthdiff>1,确实会出现问题。。。 } } 回声“
      • ”,$mpath,“\n”; $rel++;$first=true; } $cdepth=$e['depth']; } 对于($i=0;$i<$cdepth;$i++){ 回声“
      \n\n”; } 回声“
    \n”;
    编辑的代码:仍然不完美,但您可以处理它…:我做到了这一点,因此每一次深度的变化都会产生另一个李;如果新的ul来自于改变等级必须是上一个li的一部分,你必须修改一点代码。我没有在任何情况下测试它(只是快速编码)嗯,我有点马虎,现在我已经看到了预期的结果应该是什么;正在编写新代码…感谢您的代码和更新。我刚下班回家,现在我也在做这件事:)希望你能提取一些有用的东西;即使代码或多或少地复制了您给出的工作输出,但它确实是不完美的;我刚才注意到我已经添加了$veryfirst,但它应该是多余的…是的,我现在可以看到逻辑了。但实际上是业务人员填充了数组,所以我明天会检查它,看看它与实际数据的关系(秘密:他们可能还没有准备好编写我的代码:(),对我来说也很有效:)我会做一些修改,我相信mcdropdown jQuery插件可以很好地处理输出。谢谢!