0

I have the following data from a table:

+----+---------+-----+---------+--------------+
| id | name    | id2 | sname   | relation     |
+----+---------+-----+---------+--------------+
|  1 | Albaraa |   2 | Brandon | Friend       |
|  1 | Albaraa |   3 | Shen    | Friend       |
|  1 | Albaraa |   4 | Dan     | Professor    |
|  1 | Albaraa |   5 | Bob     | Boss         |
|  1 | Albaraa |   6 | Al      | God Father   |
|  2 | Brandon |   5 | Bob     | Friend       |
|  3 | Shen    |   1 | Albaraa | Friend       |
|  3 | Shen    |   2 | Brandon | Friend       |
|  3 | Shen    |   4 | Dan     | Professor    |
|  4 | Dan     |   1 | Albaraa | Student      |
|  4 | Dan     |   2 | Brandon | Student      |
|  4 | Dan     |   3 | Shen    | Student      |
|  5 | Bob     |   6 | Al      | Best Friend  |
|  6 | Al      |   1 | Albaraa | God Son      |
|  6 | Al      |   5 | Bob     | Best Friends |
+----+---------+-----+---------+--------------+

I get this data in a php array format:

Array ( 
[0] => Array ( [id] => 1 [name] => Albaraa [id2] => 2 [sname] => Brandon [relation] => Friend ) 
[1] => Array ( [id] => 1 [name] => Albaraa [id2] => 3 [sname] => Shen [relation] => Friend ) 
[2] => Array ( [id] => 1 [name] => Albaraa [id2] => 4 [sname] => Dan [relation] => Professor ) 
[3] => Array ( [id] => 1 [name] => Albaraa [id2] => 5 [sname] => Bob [relation] => Boss ) 
[4] => Array ( [id] => 1 [name] => Albaraa [id2] => 6 [sname] => Al [relation] => God Father ) 
[5] => Array ( [id] => 2 [name] => Brandon [id2] => 5 [sname] => Bob [relation] => Friend ) 
[6] => Array ( [id] => 3 [name] => Shen [id2] => 1 [sname] => Albaraa [relation] => Friend ) 
[7] => Array ( [id] => 3 [name] => Shen [id2] => 2 [sname] => Brandon [relation] => Friend ) 
[8] => Array ( [id] => 3 [name] => Shen [id2] => 4 [sname] => Dan [relation] => Professor ) 
[9] => Array ( [id] => 4 [name] => Dan [id2] => 1 [sname] => Albaraa [relation] => Student ) 
[10] => Array ( [id] => 4 [name] => Dan [id2] => 2 [sname] => Brandon [relation] => Student ) 
[11] => Array ( [id] => 4 [name] => Dan [id2] => 3 [sname] => Shen [relation] => Student ) 
[12] => Array ( [id] => 5 [name] => Bob [id2] => 6 [sname] => Al [relation] => Best Friend ) 
[13] => Array ( [id] => 6 [name] => Al [id2] => 1 [sname] => Albaraa [relation] => God Son ) 
[14] => Array ( [id] => 6 [name] => Al [id2] => 5 [sname] => Bob [relation] => Best Friends ) )

What I need to do now is set up the data I get into an array that has the first person added and from there his children are added and the children's children are added, it looks like this (made it look more readable for you guys =]):

Array ( 
[id] => 0 
[name] => Albaraa 
[children] => Array ( 
    [0] => Array ( 
    [id] => 1 
    [name] => Brandon 
    [data] => Array ( 
        [relationTo] => Albaraa 
        [relation] => Friend ) 
    [children] => Array (
        [id] => 2 
        [name] => Bob 
        [data] => Array ( 
            [relationTo] => Brandon 
            [relation] => Friend )
         [children] => Array()
    )
    ...and so on
))

I have it where it outputs the above but without the children having any children (just is an empty array) I am not really sure on how to get it to where it does this correctly, I have tried a class to do this but failed...so any help on how to get the data to look like the above one would be much appreciated!

Baraa
  • 687
  • 1
  • 9
  • 26
  • see that post http://stackoverflow.com/questions/16073251/dynamically-building-horizontal-menu/16074644#16074644 – JOE LEE May 03 '13 at 05:39

1 Answers1

0

Here's a PHP way to do it, without much optimization at all. You can probably use SQL joins to get an array that looks pretty close to the result as well.

// set root stuff
$person1 = $arr[0];

$new_arr = [];
$new_arr['id'] = 0;
$new_arr['name'] = $arr['name'];

// find children
foreach ($arr as $child){
    // if found child
    if ($child['sname'] == $person1['name']){
        // set data
        $child_arr = [];
        $child_arr['id'] = $child['id'];
        $child_arr['name'] = $child['name'];
        $child_arr['data']['relationTo'] = $child['sname'];
        $child_arr['data']['relation'] = $child['relation'];

        $new_arr['children'][] = $child_arr;

        // find grandchildren
        foreach ($arr as $gc){
            // if found grandchild
            if ($gc['sname'] == $child['name']){
                // set data
                $gc_arr = [];
                $gc_arr['id'] = $gc['id'];
                $gc_arr['name'] = $gc['name'];
                $gc_arr['data']['relationTo'] = $gc['sname'];
                $gc_arr['data']['relation'] = $gc['relation'];
                $gc_arr['children'] = []; // assuming you didn't want to go deeper

                $child_arr['children'][] = $gc_arr;
            }
        }
    }
}

If you wanted to go deeper, we would need to introduce recursion.

Steven Liao
  • 3,577
  • 3
  • 19
  • 25