Last active
May 21, 2024 15:57
-
-
Save ziishaned/c04507281a8c4b26ad39fac146b701ea to your computer and use it in GitHub Desktop.
Build a tree from a flat array in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"5a969a6fe799f7cbe9083122": { | |
"_id": "5a969a6fe799f7cbe9083122", | |
"name": "Tajawal", | |
"parent": null, | |
"created_by": "5a8d84b8e799f70a157f8211", | |
"update_at": "1519819375891", | |
"created_at": "1519819375891", | |
"children": { | |
"5a969f5ce799f7cbe9083123": { | |
"_id": "5a969f5ce799f7cbe9083123", | |
"name": "Tajawal UAE", | |
"parent": "5a969a6fe799f7cbe9083122", | |
"created_by": "5a8d84b8e799f70a157f8211", | |
"update_at": "1519820745535", | |
"created_at": "1519820636332", | |
"children": { | |
"5a96a622e799f7cbe9083129": { | |
"_id": "5a96a622e799f7cbe9083129", | |
"name": "Testing child", | |
"parent": "5a969f5ce799f7cbe9083123", | |
"created_by": "5a8d84b8e799f70a157f8211", | |
"update_at": "1519822370999", | |
"created_at": "1519822370999" | |
} | |
} | |
}, | |
"5a969ff6e799f7cbe9083126": { | |
"_id": "5a969ff6e799f7cbe9083126", | |
"name": "Tajawal Dubai", | |
"parent": "5a969a6fe799f7cbe9083122", | |
"created_by": "5a8d84b8e799f70a157f8211", | |
"update_at": "1519820790199", | |
"created_at": "1519820790199" | |
} | |
} | |
}, | |
"5a96a5c4e799f7cbe9083127": { | |
"_id": "5a96a5c4e799f7cbe9083127", | |
"name": "Android", | |
"parent": null, | |
"created_by": "5a8d84b8e799f70a157f8211", | |
"update_at": "1519822295399", | |
"created_at": "1519822276992" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
[ | |
"_id" => "5a969a6fe799f7cbe9083122", | |
"name" => "Tajawal", | |
"parent" => null, | |
"created_by" => "5a8d84b8e799f70a157f8211", | |
"update_at" => "1519819375891", | |
"created_at" => "1519819375891" | |
], | |
[ | |
"_id" => "5a969f5ce799f7cbe9083123", | |
"name" => "Tajawal UAE", | |
"parent" => "5a969a6fe799f7cbe9083122", | |
"created_by" => "5a8d84b8e799f70a157f8211", | |
"update_at" => "1519820745535", | |
"created_at" => "1519820636332" | |
], | |
[ | |
"_id" => "5a969ff6e799f7cbe9083126", | |
"name" => "Tajawal Dubai", | |
"parent" => "5a969a6fe799f7cbe9083122", | |
"created_by" => "5a8d84b8e799f70a157f8211", | |
"update_at" => "1519820790199", | |
"created_at" => "1519820790199" | |
], | |
[ | |
"_id" => "5a96a5c4e799f7cbe9083127", | |
"name" => "Android", | |
"parent" => null, | |
"created_by" => "5a8d84b8e799f70a157f8211", | |
"update_at" => "1519822295399", | |
"created_at" => "1519822276992" | |
], | |
[ | |
"_id" => "5a96a622e799f7cbe9083129", | |
"name" => "Testing child", | |
"parent" => "5a969f5ce799f7cbe9083123", | |
"created_by" => "5a8d84b8e799f70a157f8211", | |
"update_at" => "1519822370999", | |
"created_at" => "1519822370999" | |
] | |
]; | |
function buildTree(array &$elements, $parentId = 0) { | |
$branch = array(); | |
foreach ($elements as $element) { | |
if ($element['parent_id'] == $parentId) { | |
$children = buildTree($elements, $element['id']); | |
if ($children) { | |
$element['children'] = $children; | |
} | |
$branch[$element['id']] = $element; | |
unset($elements[$element['id']]); | |
} | |
} | |
return $branch; | |
} | |
var_dump(buildTree($data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How set LEVEL in array, please.