如何使用级别深度编写递归函数?

时间:2014-02-06 03:36:35

标签: javascript jquery html css

我的职能:

 public static function f($rows)
       {
           $str = '<ul>';
           $level = 1;
           foreach($rows as $row)
           {
               if($row['section_level'] > $level)
               {
                   $level = $row['section_level'];
                   // here I want to call this function again for recursion, but how??
               }
               else
               {
                   $str .= '<li><a href="#">'.$row['username'].'</a></li>';
               }
           }
           $str .= '</ul>';
           return $str;
       }

我的阵列:

array (

   array(

     ['name'] => 'test1',
     ['level'] => 1

   },
   array(

     ['name'] => 'test2',
     ['level'] => 2

   },

   array(

     ['name'] => 'test3',
     ['level'] => 2

   },
   array(

     ['name'] => 'test4',
     ['level'] => 3

   },
   array(

     ['name'] => 'test5',
     ['level'] => 3

   },
   array(

     ['name'] => 'test6',
     ['level'] => 3

   },
   array(

     ['name'] => 'test7',
     ['level'] => 3

   },



)

你可以帮我完成我的功能,按层次渲染那个数组,在每个级别增加一个<ul>,这样就像这样:

<ul>
  <li>
  <a href="#">level1</a>
    <ul>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
    </ul>
  </li>
  <li>
  <a href="#">level1</a>
    <ul>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
      <li><a href="#">level2</a></li>
    </ul>
  </li>
</ul>

2 个答案:

答案 0 :(得分:0)

Recursion的想法是建立一个数据结构树。

因此,深度并不重要,亲子关系至关重要。

这意味着在你的数组中,你需要在一个数组元素中至少有两个值,例如:

[ 'id' => 1, 'parent' => null ],
[ 'id' => 2, 'parent' => 1 ],
[ 'id' => 3, 'parent' => 1 ],
[ 'id' => 4, 'parent' => 2 ],
....

获得数据阵列后,可以使用http://hezachary.wordpress.com/2008/07/19/%E6%95%B0%E7%BB%84%E5%AD%90%E7%88%B6%E7%BA%A7%E9%80%92%E5%BD%92%EF%BC%8C%E6%A0%91%E7%8A%B6%E7%BB%93%E6%9E%84%E7%94%9F%E6%88%90/ 运行你的数组,并得到一个结构化数组。

然后你可以使用函数Recursion来导出html,例如:

function display( $ary ){
    echo '<ul>';
    foreach($ary as $data){
        echo '<li>';
        echo $data['html'];
        if( sizeof( $data['node'] ) ){
            display( $ary );
        }
        echo '</li>';
    }
    echo '</ul>';
}

以下是nest strucutre的解决方案,我根本不喜欢它:

function f($rows)
{
    $str = '';
    $level = 0;
    $first_li_mode = true;
    foreach ($rows as $row) {
        if ($level > $row['level']) {
            $str .= '</li></ul>' . "\n";
            $str .= '</li><li><a href="#">' . $row['name'] . '</a>' . "\n";
            $first_li_mode = true;
        } elseif ($row['level'] > $level) {
            $str .= '<ul><li><a href="#">' . $row['name'] . '</a>' . "\n";
            $first_li_mode = false;
            ;
        } else {
            if (!$first_li_mode) {
                $str .= '</li>' . "\n";
            } else {
                $first_li_mode = false;
            }
            $str .= '<li><a href="#">' . $row['name'] . '</a>' . "\n";
        }

        $level = $row['level'];
    }
    while ($level > 0) {
        $str .= '</li></ul>' . "\n";
        $level--;
    }
    return $str;
}


$qrr = array(
    array('name' => 'test1', 'level' => 1),
    array('name' => 'test2', 'level' => 2),
    array('name' => 'test3', 'level' => 2),
    array('name' => 'test4', 'level' => 3),
    array('name' => 'test5', 'level' => 3),
    array('name' => 'test6', 'level' => 3),
    array('name' => 'test7', 'level' => 3),
    array('name' => 'test8', 'level' => 2),
    array('name' => 'test9', 'level' => 1),
);

echo f($qrr);

答案 1 :(得分:0)

尝试嵌套循环

<?php 
function f($rows)
       {
           $str = '';
           $level = 1;
        $i=1;
           foreach($rows as $row)
           { 

         $str .="<ul><li>".$i;


            $str .="<ul><li>".$row['name']."</li><li>".$row['level']."</li></ul>";                   

              $str .="</li></ul>";$i++;
           }
           $str .= '</ul>';
           return $str;
       }

$qrr=array(

   array(
    'name' => 'test1',
     'level' => 1
   ),
   array(

     'name' => 'test2',
     'level'=> 2
   ),

   array(

     'name' => 'test3',
     'level' => 2

   ),
   array(

     'name' => 'test4',
     'level'=> 3

  ),
   array(

     'name' => 'test5',
     'level' => 3

   ),
   array(

     'name' => 'test6',
     'level'=> 3

   ),
   array(

     'name' => 'test7',
     'level' => 3

   ),



);

echo f($qrr);
?>