从数据库构建树多维数组

时间:2015-09-07 03:28:29

标签: php mysql

如何使用php和mysql构建特定的数组布局来创建无限递归循环?我已经写出了我的表格,数组布局以及我尝试完成这项工作。我花了最后几个小时研究这个但是没有运气。

我正在尝试基于我的数据库构建一个php数组,该数组在使用json_encode时构建特定的布局

我的数据库表格如下所示

TABLE `info`         |    TABLE `relations`
+--------+-----------+    +--------+-----------+
| id     | name      +    | id     | parent_id |
+--------+-----------+    +--------+-----------+
| 2p03Me | sue       |    | b5ET7N | 2p03Me    |
| b5ET7N | john      |    | h7S4bk | b5ET7N    |
| h7S4bk | bob       |    | iMz4d7 | 2p03Me    |
| iMz4d7 | sam       |    | ixRpaH | iMz4d7    |
| ixRpaH | teddy     |    | k41BhX | ixRpaH    |
| k41BhX | dan       |    +--------+-----------+ 
+--------+-----------+

生成的数组应如下所示:

Array
(
    [0] => stdClass Object
        (
            [text] => sue
            [nodes] => Array
                (
                    [0] => stdClass Object
                        (
                            [text] => john
                            [nodes] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [text] => bob
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [text] => teddy
                            [nodes] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [text] => dan
                                        )

                                )

                        )

                )

        )
)

我已经尝试了几次,但我似乎无法让它发挥作用。我的最新和最后一次尝试是

    $tree = category_tree($mysqli, $origSnippet);
    function category_tree($mysqli, $catid, $tree = array()) {
        ?><pre><?php print_r($tree); ?></pre><?php
        $sql = "SELECT * FROM `relations` INNER JOIN `info` ON `relations`.`id`=`info`.`id` WHERE `relations`.`parent_snippet_id`='$catid'";
        $children = $mysqli->query($sql);
        while($child = $children->fetch_assoc()) {
            $tree[] = array('text' => "$child[name]/$child[id]");
            echo "$child[name]/$child[snippet_id]<br/>";
            category_tree($mysqli, $child['id'], $tree);
        }
        return $tree;
    }

您可能提供的任何帮助都非常适合。

##我的问题##

如何使用php和mysql构建特定的数组布局来创建无限递归循环?我已经写出了我的表格,数组布局以及我尝试完成这项工作。我花了最后几个小时研究这个但是没有运气。

1 个答案:

答案 0 :(得分:0)

我可以通过执行以下操作来解决此问题:

$tree = array();
$node_id = 0;
$origSnippet = "2p03Me"; //THIS IS GOTTEN FROM A SEPERATE FUNCTION BUT WILL BE THE STARTING POINT OF THE LOOP

function hasChildNodes($mysqli, $snippetID) {
    $sql = "SELECT * FROM `relations` INNER JOIN `info` ON `relations`.`id`=`info`.`id` WHERE `relations`.`parent_id`='$snippetID'";
    return $mysqli->query($sql)->num_rows;
}

function buildTree($mysqli, $snippetID, $node_id, &$tree) {
    $sql = "SELECT * FROM `relations` INNER JOIN `info` ON `relations`.`id`=`info`.`id` WHERE `relations`.`parent_id`='$snippetID'";
    $snippets = $mysqli->query($sql);
    while($snippet = $snippets->fetch_assoc()) {
        $tree[$node_id] = Array("text" => $snippet['name']);
        if (hasChildNodes($mysqli, $snippet['id']) >= 1) {
            buildTree($mysqli, $snippet['id'], 0, $tree[$node_id]['nodes']);
        }
        $node_id++;
    }
}

$sql = "SELECT * FROM `info` WHERE `id`='$origSnippet' LIMIT 1";
$origSnippet = $mysqli->query($sql)->fetch_assoc();

$tree[$node_id] = Array("text" => $origSnippet['name']);

if (hasChildNodes($mysqli, $childSnippet) >= 1) {
    buildTree($mysqli, $childSnippet, $node_id, $tree[$node_id]['nodes']);
}