PHP - Echo multidimentional数组作为HTML列表

时间:2015-07-20 11:02:37

标签: php html recursion multidimensional-array

我正在寻找一个函数,它接受一系列页面/类别(从平面数据库结果)和echo到基于父ID的嵌套页面/类别<ul>(HTML无序列表)项目。我想以递归方式执行此操作,以便可以完成任何级别的嵌套。

+-------+---------------+---------------------------+
|   id  |   parent_id   |           title           |
+-------+---------------+---------------------------+
|   1   |       0       |   Parent Page             |
|   2   |       1       |   Sub Page                |
|   3   |       2       |   Sub Sub Page            |
|   4   |       0       |   Another Parent Page     |
+-------+---------------+---------------------------+

这是我想在我的视图文件中处理的数组:

Array
(
[0] => Array
    (
        [id] => 1
        [parent_id] => 0
        [title] => Parent Page
        [children] => Array
                    (
                        [0] => Array
                            (
                                [id] => 2
                                [parent_id] => 1
                                [title] => Sub Page
                                [children] => Array
                                            (
                                                [0] => Array
                                                    (
                                                        [id] => 3
                                                        [parent_id] => 1
                                                        [title] => Sub Sub Page
                                                    )
                                            )
                            )
                    )
    )
[1] => Array
    (
        [id] => 4
        [parent_id] => 0
        [title] => Another Parent Page
    )
)

我发现this文章 SIMILAR 但是 NOT DUPLICATE 到我发布的那篇文章,这是我发现以下功能可以帮助的地方答案。

function buildTree(array $elements, $parentId = 0) {
$branch = array();

foreach ($elements as $element) {
    if ($element->parent_id == $parentId) {
        $children = buildTree($elements, $element->id);
        if ($children) {
            $element->children = $children;
        }
        $branch[] = $element;
    }
}

return $branch;
}

$tree = buildTree($rows);

修改

我想回应以下结构中的数据:

<ul>
  <li><a href="#">Parent Page</a>
    <ul>
      <li><a href="#">Sub Page</a>
        <ul>
          <li><a href="#">Sub Sub Page</a>
        </ul>
        </li>
    </ul>
    </li>
    <li><a href="#">Another Parent Page</a>
    </li>
</ul>

2 个答案:

答案 0 :(得分:1)

  

尝试此功能并根据您的要求进行更改:

function buildTree(Array $data, $parent = 0) {
$tree = array();
foreach ($data as $d) {
    if ($d['parent'] == $parent) {
        $children = buildTree($data, $d['id']);
        // set a trivial key
        if (!empty($children)) {
            $d['_children'] = $children;
        }
        $tree[] = $d;
    }
}
return $tree;
}

function printTree($tree, $r = 0, $p = null) { 
foreach ($tree as $i => $t) {
    $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' '; 
    printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']);
    if ($t['parent'] == $p) {
        // reset $r
       $r = 0; 
    }
    if (isset($t['_children'])) {
        printTree($t['_children'],$sel, ++$r, $t['parent']);
    }
}
}
$rows = array('your array');

$tree = buildTree($rows);

print("<select name='selectionParentpage' class='just_textfield' id='selectionParentpage'><option value=''>--Select Page--</option>\n");
printTree($tree);
print("</select>");

答案 1 :(得分:0)

以下是您可以为视图部分执行的操作

sqldf

您可以使用类似的东西来获取树视图数组:

library(sqldf)
sqldf('select substr(a, 1, instr(a, ".")-1) as a1,
        sum(b) as b
        from df1 
        group by a1')
#   a1 b
#1  A 4
#2  B 5