Skip to content

Instantly share code, notes, and snippets.

@ziishaned
Last active May 21, 2024 15:57
Show Gist options
  • Save ziishaned/c04507281a8c4b26ad39fac146b701ea to your computer and use it in GitHub Desktop.
Save ziishaned/c04507281a8c4b26ad39fac146b701ea to your computer and use it in GitHub Desktop.
Build a tree from a flat array in PHP
{
"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"
}
}
<?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));
@ydarwin
Copy link

ydarwin commented Aug 14, 2023

How set LEVEL in array, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment