@bytesnz

Jack Farley, Web Application Engineer

I'm Making Hierarchies

Something I have often needed to do in PHP is create hierarchical structures from an array. Usually I do this with recursive functions, but today I thought I would try something different - using references.

I needed to do it for a list of directories containing images, the hierarchical structure was created to resemble the folder structure the folders came from - folders with sub-folders.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
function getFolderHierarchy($folders) {
$hierarchy = array();
$parents = array();
$p = 0;
$current =& $hierarchy;
$depth = 1;
$previous = null;
foreach ($folders as $folder) {
$path = explode(DIRECTORY_SEPARATOR, $folder['dir']);
$pCount = count($path);
if ($pCount > $depth) { // Sub-directory
$parents[$p++] =& $current;
$current =& $previous;
$depth = $pCount;
} else if ($pCount < $depth) {
$current =&amp; $parents[--$p];
$depth = $pCount;
}
$current[$path[$pCount-1]] = array(
null => $folder['id']
);
$previous =&amp; $current[$path[$pCount-1]];
}
return $hierarchy;
}
$folders = array(
array(
'dir' => 'South America',
'id' => '1'
),
array(
'dir' => 'South America/Ecuador',
'id' => '4'
),
array(
'dir' => 'South America/Ecuador/Alausi',
'id' => '29'
),
array(
'dir' => 'South America/Ecuador/Alausi/Nariz del Diablo',
'id' => '30'
),
array(
'dir' => 'South America/Ecuador/Amazon',
'id' => '31'
),
array(
'dir' => 'South America/Ecuador/Banos',
'id' => '10'
),
array(
'dir' => 'South America/Ecuador/Banos/Canyoning',
'id' => '11'
),
array(
'dir' => 'South America/Ecuador/Cotopaxi National Park',
'id' => '16'
),
array(
'dir' => 'South America/Ecuador/Cotopaxi National Park/Cotopaxi',
'id' => '19'
),
array(
'dir' => 'South America/Ecuador/Cotopaxi National Park/Ruminahui',
'id' => '18'
),
array(
'dir' => 'South America/Ecuador/Cotopaxi National Park/Secret Garden Cotopaxi',
'id' => '17'
),
array(
'dir' => 'South America/Ecuador/Cuenca',
'id' => '21'
),
array(
'dir' => 'South America/Ecuador/Cuenca/Cajas National Park',
'id' => '23'
),
array(
'dir' => 'South America/Ecuador/Cuenca/Ingapirca',
'id' => '22'
),
array(
'dir' => 'South America/Ecuador/Mindo',
'id' => '24'
),
array(
'dir' => 'South America/Ecuador/Mindo/El Descanso',
'id' => '26'
),
array(
'dir' => 'South America/Ecuador/Mindo/El Quetzal',
'id' => '27'
),
array(
'dir' => 'South America/Ecuador/Mindo/Mariposas de Mindo',
'id' => '28'
),
array(
'dir' => 'South America/Ecuador/Mindo/Tarabita',
'id' => '25'
),
);
print_r(getFolderHierarchy($folders));

The following is returned when you run the above code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Array
(
[South America] => Array
(
[] => 1
[Ecuador] => Array
(
[] => 4
[Alausi] => Array
(
[] => 29
[Nariz del Diablo] => Array
(
[] => 30
)
)
[Amazon] => Array
(
[] => 31
)
[Banos] => Array
(
[] => 10
[Canyoning] => Array
(
[] => 11
)
)
[Cotopaxi National Park] => Array
(
[] => 16
[Cotopaxi] => Array
(
[] => 19
)
[Ruminahui] => Array
(
[] => 18
)
[Secret Garden Cotopaxi] => Array
(
[] => 17
)
)
[Cuenca] => Array
(
[] => 21
[Cajas National Park] => Array
(
[] => 23
)
[Ingapirca] => Array
(
[] => 22
)
)
[Mindo] => Array
(
[] => 24
[El Descanso] => Array
(
[] => 26
)
[El Quetzal] => Array
(
[] => 27
)
[Mariposas de Mindo] => Array
(
[] => 28
)
[Tarabita] => Array
(
[] => 25
)
)
)
)
)