开发者

How to display many nested lists using PHP?

I was wondering how can you generate lists that have many nested lists deep using PHP.

How would you go about coding the PHP.

I'm stumpe开发者_如何转开发d please help I'm new to PHP.


Here's one method:

$tree = array(
  1,  2,
  array(31, 32, array(331, 332, 333)), array(341, 342),
  4, array(51, 52, 53, 54, array(551, 552, 553, array(5541, 5542))),
);
render_tree($tree);

function render_tree($tree, $indent = 0) {
  $space = str_repeat(' ', $indent);
  echo "$space<ul>\n";
  foreach ($tree as $node) {
    render_node($node, $indent + 2);
  }
  echo "$space</ul>\n";
}

function render_node($node, $indent) {
  $space = str_repeat(' ', $indent);
  if (is_array($node) && count($node) > 0) {
    echo "$space<li>\n";
    render_tree($node, $indent + 2);
    echo "$space</li>\n";
  } else {
    echo "$space<li>$node</li>\n";
  }
}


Perhaps this function (print_r)

(This answers the question in the title. For the others, please consult a book/tutorial. Please use the resources available.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜