fork(1) download
  1. <?php
  2. $array = array(
  3. "id" => 1,
  4. "name" => "Europe",
  5. "path" => "/"
  6. ),
  7. "id" => 2,
  8. "name" => "Germany",
  9. "path" => "/1/"
  10. ),
  11. "id" => 3,
  12. "name" => "France",
  13. "path" => "/1/"
  14. ),
  15. "id" => 4,
  16. "name" => "Berlin",
  17. "path" => "/1/2/"
  18. ),
  19. "id" => 5,
  20. "name" => "Munich",
  21. "path" => "/1/2/"
  22. ),
  23. "id" => 6,
  24. "name" => "Asia",
  25. "path" => "/"
  26. ),
  27. "id" => 7,
  28. "name" => "China",
  29. "path" => "/6/"
  30. ),
  31. "id" => 8,
  32. "name" => "Bangladesh",
  33. "path" => "/6/"
  34. ),
  35. "id" => 9,
  36. "name" => "Beijing",
  37. "path" => "/6/7/"
  38. ),
  39. "id" => 10,
  40. "name" => "Dhaka",
  41. "path" => "/6/8/"
  42. ),
  43. "id" => 11,
  44. "name" => "Berlin - District 1",
  45. "path" => "/1/2/4/"
  46. ),
  47. "id" => 12,
  48. "name" => "Berlin - District 2",
  49. "path" => "/1/2/4/"
  50. ),
  51. "id" => 13,
  52. "name" => "Berlin - Sub-District 1",
  53. "path" => "/1/2/4/11/"
  54. ),
  55. "id" => 14,
  56. "name" => "Berlin - Sub-Sub-District 1",
  57. "path" => "/1/2/4/11/13/"
  58. ),
  59. );
  60. function pathToTree($array){
  61. $tree = array();
  62. foreach($array AS $item) {
  63. $pathIds = explode("/", ltrim($item["path"], "/") . $item["id"]);
  64. $current = &$tree;
  65. foreach($pathIds AS $id) {
  66. if(!isset($current["childs"][$id])) $current["childs"][$id] = array();
  67. $current = &$current["childs"][$id];
  68. if($id == $item["id"]) {
  69. $current = $item;
  70. }
  71. }
  72. }
  73. return $tree;
  74. }
  75. print_r(pathToTree($array));
Success #stdin #stdout 0.04s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [childs] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [name] => Europe
                    [path] => /
                    [childs] => Array
                        (
                            [2] => Array
                                (
                                    [id] => 2
                                    [name] => Germany
                                    [path] => /1/
                                    [childs] => Array
                                        (
                                            [4] => Array
                                                (
                                                    [id] => 4
                                                    [name] => Berlin
                                                    [path] => /1/2/
                                                    [childs] => Array
                                                        (
                                                            [11] => Array
                                                                (
                                                                    [id] => 11
                                                                    [name] => Berlin - District 1
                                                                    [path] => /1/2/4/
                                                                    [childs] => Array
                                                                        (
                                                                            [13] => Array
                                                                                (
                                                                                    [id] => 13
                                                                                    [name] => Berlin - Sub-District 1
                                                                                    [path] => /1/2/4/11/
                                                                                    [childs] => Array
                                                                                        (
                                                                                            [14] => Array
                                                                                                (
                                                                                                    [id] => 14
                                                                                                    [name] => Berlin - Sub-Sub-District 1
                                                                                                    [path] => /1/2/4/11/13/
                                                                                                )

                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                            [12] => Array
                                                                (
                                                                    [id] => 12
                                                                    [name] => Berlin - District 2
                                                                    [path] => /1/2/4/
                                                                )

                                                        )

                                                )

                                            [5] => Array
                                                (
                                                    [id] => 5
                                                    [name] => Munich
                                                    [path] => /1/2/
                                                )

                                        )

                                )

                            [3] => Array
                                (
                                    [id] => 3
                                    [name] => France
                                    [path] => /1/
                                )

                        )

                )

            [6] => Array
                (
                    [id] => 6
                    [name] => Asia
                    [path] => /
                    [childs] => Array
                        (
                            [7] => Array
                                (
                                    [id] => 7
                                    [name] => China
                                    [path] => /6/
                                    [childs] => Array
                                        (
                                            [9] => Array
                                                (
                                                    [id] => 9
                                                    [name] => Beijing
                                                    [path] => /6/7/
                                                )

                                        )

                                )

                            [8] => Array
                                (
                                    [id] => 8
                                    [name] => Bangladesh
                                    [path] => /6/
                                    [childs] => Array
                                        (
                                            [10] => Array
                                                (
                                                    [id] => 10
                                                    [name] => Dhaka
                                                    [path] => /6/8/
                                                )

                                        )

                                )

                        )

                )

        )

)