<?php
"id" => 1,
"name" => "Europe",
"path" => "/"
),
"id" => 2,
"name" => "Germany",
"path" => "/1/"
),
"id" => 3,
"name" => "France",
"path" => "/1/"
),
"id" => 4,
"name" => "Berlin",
"path" => "/1/2/"
),
"id" => 5,
"name" => "Munich",
"path" => "/1/2/"
),
"id" => 6,
"name" => "Asia",
"path" => "/"
),
"id" => 7,
"name" => "China",
"path" => "/6/"
),
"id" => 8,
"name" => "Bangladesh",
"path" => "/6/"
),
"id" => 9,
"name" => "Beijing",
"path" => "/6/7/"
),
"id" => 10,
"name" => "Dhaka",
"path" => "/6/8/"
),
"id" => 11,
"name" => "Berlin - District 1",
"path" => "/1/2/4/"
),
"id" => 12,
"name" => "Berlin - District 2",
"path" => "/1/2/4/"
),
"id" => 13,
"name" => "Berlin - Sub-District 1",
"path" => "/1/2/4/11/"
),
"id" => 14,
"name" => "Berlin - Sub-Sub-District 1",
"path" => "/1/2/4/11/13/"
),
);
function pathToTree($array){
foreach($array AS $item) {
$pathIds = explode("/", ltrim($item["path"], "/") . $item["id"]); $current = &$tree;
foreach($pathIds AS $id) {
if(!isset($current["childs"][$id])) $current["childs"][$id] = array(); $current = &$current["childs"][$id];
if($id == $item["id"]) {
$current = $item;
}
}
}
return $tree;
}
PD9waHAKJGFycmF5ID0gYXJyYXkoCglhcnJheSgKCQkiaWQiICAgPT4gMSwKCQkibmFtZSIgPT4gIkV1cm9wZSIsCgkJInBhdGgiID0+ICIvIgoJKSwKCWFycmF5KAoJCSJpZCIgICA9PiAyLAoJCSJuYW1lIiA9PiAiR2VybWFueSIsCgkJInBhdGgiID0+ICIvMS8iCgkpLAoJYXJyYXkoCgkJImlkIiAgID0+IDMsCgkJIm5hbWUiID0+ICJGcmFuY2UiLAoJCSJwYXRoIiA9PiAiLzEvIgoJKSwKCWFycmF5KAoJCSJpZCIgICA9PiA0LAoJCSJuYW1lIiA9PiAiQmVybGluIiwKCQkicGF0aCIgPT4gIi8xLzIvIgoJKSwKCWFycmF5KAoJCSJ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