用于邻接列表显示的递归PHP函数

Ale*_*Mcp 0 php recursion adjacency-list hierarchical-data

我有一个像这样的数据库:

id  text           parent
1   Parent 1        0   
2   Child of 1      1   
3   Sibling         1   
4   Another Parent  0 
5   A first child   4
Run Code Online (Sandbox Code Playgroud)

所以我试图捕捉一个树形结构,我列出了父母.我知道另一个选项(我认为是嵌套的吗?)但是我现在要坚持这个.我现在正试图将数据从数据库中取出并放入PHP中的嵌套数组结构中.我有这样的功能:

class Data_Manager
{   
    public $connection = '';
    public $collection = array();

    function __construct() {
        $this->connection = mysql_connect('localhost', 'root', 'root');
        $thisTable = mysql_select_db('data');
            // error handling truncated
    }


    function get_all() {
        $arr = &$this->collection;

        $this->recurseTree('', 0, $arr);
        var_dump($arr);
    }

    function recurseTree($parent, $level, $arrayNode) {
        $result = mysql_query('SELECT * FROM tasks WHERE parent="' . $parent . '";');

        while ($row = mysql_fetch_array($result)) {
            $row['children'] = array(); //where I'd like to put the kids    
            $arrayNode[$row['id']]= $row;
            $this->recurseTree($row['id'], $level+1, $arrayNode[$row['id']]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

所以我想提出的是某种关联数组的嵌套树,但我无法弄清楚如何做到这一点.似乎没有什么东西可以写入我传入的数组中,而且我在递归中有点失去了对自己的追踪.任何人都可以帮助我完成最后一次驼峰,这将导致类似于:

[
Parent1 => [
               children => ['Child of 1', 'Sibling']
           ],
AnotherParent => [
                     children => ['First Child']
                 ]
]
Run Code Online (Sandbox Code Playgroud)

而且我不太关心输出的具体形式.它将变成JSON,我还没有处理编写客户端处理程序,所以不用担心确切的结构.

谢谢!

jon*_*tar 5

试试这个.

$sql = "SELECT * FROM tasks";
$r = mysql_query($sql, $conn);
$arr = array();
while ($row = mysql_fetch_assoc($r))
   $arr[] = $row

function build($arrayIn, $parent)
{
    $makeFilter = function($p) {return function($x) use ($p) {return $x['parent'] == $p;};};
    $f = $makeFilter($parent);
    $these = array_filter($arrayIn, $f);
    $remaining = array_diff_assoc($arrayIn, $these);
    $ans = array();

    foreach($these as $cur)
    {
       $ans[$cur['text']] = build($remaining, $cur['id']);
    }
    return $ans ? $ans : null;
}

$tree = build($arr, 0)
echo_r($arr);
echo "becomes<br />";
echo_r($tree);
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

Array
(
[0] => Array
    (
        [text] => a
        [id] => 1
        [parent] => 0
    )

[1] => Array
    (
        [text] => b
        [id] => 2
        [parent] => 0
    )

[2] => Array
    (
        [text] => c
        [id] => 3
        [parent] => 1
    )

[3] => Array
    (
        [text] => d
        [id] => 4
        [parent] => 2
    )

[4] => Array
    (
        [text] => e
        [id] => 5
        [parent] => 2
    )

[5] => Array
    (
        [text] => f
        [id] => 6
        [parent] => 3
    )

)

becomes

Array
(
[a] => Array
    (
        [c] => Array
            (
                [f] => 
            )

    )

[b] => Array
    (
        [d] => 
        [e] => 
    )

)
Run Code Online (Sandbox Code Playgroud)