Skip to content

Instantly share code, notes, and snippets.

@zmiftah
Created March 12, 2015 09:25
Show Gist options
  • Save zmiftah/efa1e054eb63d2f09b7c to your computer and use it in GitHub Desktop.
Save zmiftah/efa1e054eb63d2f09b7c to your computer and use it in GitHub Desktop.
How to Create Treelike Structure
protected function traverseData($programs, $parentId=0)
{
$data = array();
foreach ($programs as $id => $program) {
if ($program['parent'] == $parentId) {
$nodes = $this->traverseData($programs, $program['id']);
$obj = new StdClass;
$obj->text = $program['name'];
$obj->icon = 'fa fa-file';
$obj->href = '#program-'.$program['id'];
if ($nodes) { $obj->nodes = $nodes; }
$data[] = $obj;
}
}
return $data;
}
Result:
[
{
"text":"SEN",
"icon":"fa fa-file",
"href":"#program-54",
"nodes":
[
{
...
}
]
},
...
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment