PHP数组的层次结构

3

i have a table like this:

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

我需要获取像这样的分层数据数组。最好的方法是什么?
Array
(
    [1] => Array
        (
            [3] => Array
                (
                    [4] => 
                )

        )

    [2] => 
)

please help me.

3个回答

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

虽然解决方案是正确的,但如果层次结构更深,则会失败。那时就需要使用递归函数。如果我错了,请纠正我。 - Alp

2
我会使用像Propel这样的ORM,它内置了对你所要求的功能的支持。其中ORMPropel是链接。

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

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接