javascript - How to write a recursive function using levels depth? -
my function:
public static function f($rows) { $str = '<ul>'; $level = 1; foreach($rows $row) { if($row['section_level'] > $level) { $level = $row['section_level']; // here want call function again recursion, how?? } else { $str .= '<li><a href="#">'.$row['username'].'</a></li>'; } } $str .= '</ul>'; return $str; }
my array:
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 }, )
can please me complete function render array levels, adding each level <ul>
, this:
<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>
the idea recursion build data structure tree.
therefore, depth not important, parent - child relationship essential.
that means in array, need @ least have 2 value in 1 array element, example:
[ 'id' => 1, 'parent' => null ], [ 'id' => 2, 'parent' => 1 ], [ 'id' => 3, 'parent' => 1 ], [ 'id' => 4, 'parent' => 2 ], ....
once have data array, can use 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/ run array, , structured array.
then can use function recursion export html, such as:
function display( $ary ){ echo '<ul>'; foreach($ary $data){ echo '<li>'; echo $data['html']; if( sizeof( $data['node'] ) ){ display( $ary ); } echo '</li>'; } echo '</ul>'; }
here solution nest strucutre, not @ all:
function f($rows) { $str = ''; $level = 0; $first_li_mode = true; foreach ($rows $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);
Comments
Post a Comment