php数组层次结构

php数组层次结构,php,sql,database,Php,Sql,Database,我有一张这样的桌子: +----+--------+-------+ | id | parent | title | +----+--------+-------+ | 1 | NULL | yek | | 2 | NULL | do | | 3 | 1 | se | | 4 | 3 | char | +----+--------+-------+ 我需要得到这样的层次数据数组。最好的方法是什么 Array ( [1] =>

我有一张这样的桌子:

+----+--------+-------+
| id | parent | title |
+----+--------+-------+
|  1 |   NULL | yek   |
|  2 |   NULL | do    |
|  3 |      1 | se    |
|  4 |      3 | char  |
+----+--------+-------+
我需要得到这样的层次数据数组。最好的方法是什么

Array
(
    [1] => Array
        (
            [3] => Array
                (
                    [4] => 
                )

        )

    [2] => 
)

请帮帮我。

我想使用一个like,它内置了对您要求的功能的支持。

首先,一个新的数组,其中键显示为id。然后,这个数组构建了一个图。它发生在递归图中。(对不起我的英语)

// dummy data $recordset should be retrieved from db
$recordset = array(array('id'=>1, 'parent'=>NULL, 'title'=>'yek'),
                   array('id'=>2, 'parent'=>NULL, 'title'=>'do'),
                   array('id'=>3, 'parent'=>1, 'title'=>'se'),
                   array('id'=>4, 'parent'=>3, 'title'=>'char'),
                  );

function make_tree($recordset)
{
    $tree = array();
    foreach($recordset as $record) {
        if ($record['parent'] !== NULL) {
            if (!array_key_exists($record['parent'], $tree) $tree[$record['parent']] = array('record'=>array(), 'children'=>array());
            $tree[$record['parent']]['children'][$record['id']] = $record;
        } else {
            if (!array_key_exists($record['id'], $tree) $tree[$record['id']] = array('record'=>array(), 'children'=>array());
            $tree[$record['id']] = $record;
        }
    }

    return $tree;
}

尽管解决方案是正确的,但如果层次结构更深入,它将失败。这时需要一个递归函数。如果我错了,请纠正我
<?php

function change_index_to_id($array) {
    $result = array();

    foreach ($array as $value) {
        $result[$value['id']] = $value;
    }

    return $result;
}

function make_graph($data) {
    $graph = array();

    foreach ($data as $id => $value) {
        if (!is_null($value['parent'])) {
            $graph[$value['parent']][$id] = true;
        } else {
            $graph[$id] = array();
        }
    }

    return $graph;
}

function make_hierarchical_array($item_id, $graph, $data, $marked_items) {  
    $result = $data[$item_id];
    $marked_items[$item_id] = true;

    foreach ($graph[$item_id] as $id => $v) {
        if (isset($graph[$id]) && ! $marked_items[$id]) {
            $result['childrens'][$id] = make_hierarchical_array($id, $graph, $data, &$marked_items);
        } else {
            $result['childrens'][$id] = $data[$id];
        }
    }

    return $result;
}

// load data from database or other
$data = array(
    array(
        'id' => 1,
        'parent' => null,
        'title' => 'yek'
    ),
    array(
        'id' => 2,
        'parent' => null,
        'title' => 'do'
    ),
    array(
        'id' => 3,
        'parent' => 1,
        'title' => 'se'
    ),
    array(
        'id' => 4,
        'parent' => 3,
        'title' => 'char'
    ),
);


$data = change_index_to_id($data);
$graph = make_graph($data);

$result = array();
$marked_items = array();
foreach ($graph as $id => $childs) {
    if ($marked_items[$id] == false) {
        $result[$id] = make_hierarchical_array($id, $graph, $data, &$marked_items);
    }
}
print_r($result);

?>