php数组分层

时间:2011-06-18 19:25:31

标签: php sql database

我有一张这样的桌子:

+----+--------+-------+
| id | parent | title |
+----+--------+-------+
|  1 |   NULL | yek   |
|  2 |   NULL | do    |
|  3 |      1 | se    |
|  4 |      3 | char  |
+----+--------+-------+

我需要得到像这样的Hierarchical数据数组。什么是最好的方式?

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

        )

    [2] => 
)

请帮帮我。

3 个答案:

答案 0 :(得分:2)

我会使用ORM之类的Propel,它内置支持您要求的功能。

答案 1 :(得分:2)

// 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;
}

答案 2 :(得分:0)

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

<?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);

?>