3

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.

0

3 Answers 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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

although the solution is correct, it will fail if the hierarchy is going deeper. a recursive function would be needed then. correct me if i'm wrong
2

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

Comments

0

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

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.