Вопрос

MAIN QUESTION: What is the most efficient way to transform a flat table to a tree like hierarchical structure ?

CONTEXT: Am a bit rusted and don't really know if this should be handled on SQL, PHP or even JavaScript (Kendo UI) side !

For a Kendo TreeView (HierachicalDataSource), i must export a json encoded tree structure from a table made from a single Select but with multiple JOINS to multiple tables... It generates a huge table of the following logical structure:

Source Table My goal is to group by "date", "client", "employee" and to then list the "events". I need to get a structure like this one:

Desired tree like structure from flat table I guess the best is a series of PHP loops ?... What could the code look like ?

Thanks !

Это было полезно?

Решение

Try this PHP code:

$result = array();
foreach($data as $row) {
    $date = $row['date'];
    $employee = $row['employee'];
    $client = $row['client'];
    $event = $row['event'];

    $result[$date][$client][$employee][] = $event;
}

I suppose you get $data through a simple query before this code.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top