php array hierarchical

i have a table like this:

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

and i need to get array of Hierarchical data like this. what is the best way ?

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

        )

    [2] => 
)

please help me.

Upvotes: 3

Views: 577

Answers (3)

Noxt
Noxt

Reputation: 186

First, a new array in which keys appear as id's. Then, this array built graph. And it happens recursive the graph. (sorry for my english)

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

?>

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72672

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

Upvotes: 2

Alp
Alp

Reputation: 29739

I would use a ORM like Propel, which has built-in support for the feature you ask for.

Upvotes: 2

Related Questions