1
+ <?php
2
+ /**
3
+ * Created by PhpStorm.
4
+ * User: bert
5
+ * Date: 4/10/18
6
+ * Time: 15:17
7
+ */
8
+
9
+ $ array = [
10
+ ["id " => 1 , "categoryName " => "First " , "parentCategory " => 0 , "SortInd " => 0 ],
11
+ ["id " => 2 , "categoryName " => "Second " , "parentCategory " => 1 , "SortInd " => 0 ],
12
+ ["id " => 3 , "categoryName " => "Third " , "parentCategory " => 1 , "SortInd " => 1 ],
13
+ ["id " => 4 , "categoryName " => "Fourth " , "parentCategory " => 3 , "SortInd " => 0 ],
14
+ ["id " => 5 , "categoryName " => "Fifth " , "parentCategory " => 4 , "SortInd " => 0 ],
15
+ ["id " => 6 , "categoryName " => "Sixth " , "parentCategory " => 5 , "SortInd " => 0 ],
16
+ ["id " => 7 , "categoryName " => "Seventh " , "parentCategory " => 6 , "SortInd " => 0 ],
17
+ ["id " => 8 , "categoryName " => "Eight " , "parentCategory " => 7 , "SortInd " => 0 ],
18
+ ["id " => 9 , "categoryName " => "Ninth " , "parentCategory " => 1 , "SortInd " => 0 ],
19
+ ["id " => 10 , "categoryName " => "Tenth " , "parentCategory " => 2 , "SortInd " => 1 ],
20
+ ];
21
+
22
+ foreach ($ array as $ row ){
23
+ $ categories [$ row ["parentCategory " ]][] = $ row ;
24
+ }
25
+
26
+ showCategoryTree ($ categories , 0 );
27
+
28
+ function showCategoryTree (array $ categories , int $ n ) {
29
+ if (isset ($ categories [$ n ])) {
30
+ foreach ($ categories [$ n ] as $ category ) {
31
+ echo str_repeat ("- " , $ n ) . "" . $ category ["categoryName " ] . PHP_EOL ;
32
+ showCategoryTree ($ categories , $ category ["id " ]);
33
+ }
34
+ }
35
+ return ;
36
+ }
0 commit comments