我的功能:

 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;
       }

我的数组:

您能否帮助我完成我的功能,以便按级别渲染该数组,并将其添加到每个级别acc>,如下所示:
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

   },



)

最佳答案

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

因此,深度并不重要,父母与孩子的关系至关重要。

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

[ '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/
运行您的阵列,并获得结构化的阵列。

那么您可以使用递归函数导出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>';
}


这是巢状结构的解决方案,我一点都不喜欢:

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);

关于javascript - 如何使用级别深度编写递归函数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21593502/

10-10 21:59