質問編集履歴
2
情報がたりなかったため
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,71 +1,79 @@
|
|
1
1
|
### 実現したいこと
|
2
2
|
PHPでフラットな配列を木構造に変換した際に経路を残したいです。
|
3
|
+
配列から木構造に変換はできたのですがキーのlistとして残す部分の処理が分かりません。
|
3
4
|
|
4
5
|
### 該当のソースコード
|
5
6
|
```
|
6
|
-
|
7
|
+
// 変換前
|
7
|
-
[1700] => Array
|
8
|
-
|
8
|
+
[
|
9
|
-
[to_id] => 1700
|
10
|
-
|
9
|
+
'id' => 1,
|
10
|
+
'parent_id' => null
|
11
|
+
],
|
11
|
-
|
12
|
+
[
|
12
|
-
[1800] => Array
|
13
|
-
(
|
14
|
-
|
13
|
+
'id' => 2,
|
14
|
+
'parent_id' => 1
|
15
|
+
],
|
16
|
+
[
|
17
|
+
'id' => 3,
|
18
|
+
'parent_id' => 1
|
19
|
+
],
|
20
|
+
[
|
21
|
+
'id' => 4,
|
22
|
+
'parent_id' => 3
|
23
|
+
],
|
24
|
+
[
|
25
|
+
'id' => 5,
|
26
|
+
'parent_id' => null
|
27
|
+
],
|
28
|
+
[
|
29
|
+
'id' => 6,
|
30
|
+
'parent_id' => 5
|
31
|
+
],
|
32
|
+
[
|
15
|
-
|
33
|
+
'id' => 7,
|
16
|
-
)
|
17
|
-
[1900] => Array
|
18
|
-
(
|
19
|
-
|
34
|
+
'parent_id' => 6
|
35
|
+
];
|
36
|
+
|
37
|
+
// 変換後(木構造)
|
38
|
+
[
|
39
|
+
'list' => '1',
|
20
|
-
|
40
|
+
'id' => 1,
|
41
|
+
'parent_id' => null,
|
42
|
+
'children' => [
|
21
|
-
|
43
|
+
[
|
44
|
+
'list' => '1, 2',
|
45
|
+
'id' => 2,
|
46
|
+
'parent_id' => 1
|
47
|
+
],
|
48
|
+
[
|
49
|
+
'list' => '1, 3',
|
50
|
+
'id' => 3,
|
51
|
+
'parent_id' => 1,
|
52
|
+
'children' => [
|
53
|
+
'list' => '1, 3, 4',
|
54
|
+
'id' => 4,
|
55
|
+
'parent_id' => 3
|
56
|
+
]
|
57
|
+
],
|
58
|
+
],
|
59
|
+
[
|
60
|
+
'id' => 5,
|
61
|
+
'parent_id' => null,
|
62
|
+
'children' => [
|
63
|
+
[
|
64
|
+
'list' => '5, 6',
|
65
|
+
'id' => 6,
|
66
|
+
'parent_id' => 5
|
67
|
+
],
|
68
|
+
[
|
69
|
+
'list' => '5, 7',
|
70
|
+
'id' => 7,
|
71
|
+
'parent_id' => 6
|
72
|
+
]
|
73
|
+
]
|
74
|
+
],
|
75
|
+
];
|
22
76
|
```
|
23
|
-
|
24
|
-
```PHP
|
25
|
-
public function tree($data){
|
26
|
-
$tree = [];
|
27
|
-
$index = [];
|
28
|
-
|
29
|
-
foreach($data as $id => $value){
|
30
|
-
$index[$id] = array(
|
31
|
-
'id' => $id,
|
32
|
-
'parent' => $value['parent'],
|
33
|
-
'children' => array[],
|
34
|
-
);
|
35
|
-
}
|
36
|
-
|
37
|
-
foreach($index as $key => $value){
|
38
|
-
if(array_key_exists($value['parent'], $index) && $value['parent'] != $value['id']){
|
39
|
-
$index[$value['parent']]['children'][] = & $index[$key];
|
40
|
-
}else{
|
41
|
-
$tree[] = & $index[$key];
|
42
|
-
}
|
43
|
-
}
|
44
|
-
|
45
|
-
return $tree;
|
46
|
-
}
|
47
|
-
```
|
48
|
-
|
49
|
-
```
|
50
|
-
// listに経路を残す
|
51
|
-
[0] => Array
|
52
|
-
(
|
53
|
-
[list] => 1600,1700
|
54
|
-
[to_id] => 1700
|
55
|
-
[from_id] => 1600
|
56
|
-
[children] => Array(
|
57
|
-
[list] => 1600,1700,1800
|
58
|
-
[to_id] => 1800
|
59
|
-
[from_id] => 1700
|
60
|
-
[children] => Array(
|
61
|
-
[list] => 1600,1700,1800.1900
|
62
|
-
[to_id] => 1900
|
63
|
-
[from_id] => 1800
|
64
|
-
)
|
65
|
-
)
|
66
|
-
)
|
67
|
-
```
|
68
|
-
|
69
77
|
|
70
78
|
### 試したこと
|
71
79
|
・親をみて足していく
|
1
木構造になっていなかったのを修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,23 +48,21 @@
|
|
48
48
|
|
49
49
|
```
|
50
50
|
// listに経路を残す
|
51
|
-
[
|
51
|
+
[0] => Array
|
52
52
|
(
|
53
|
-
|
53
|
+
[list] => 1600,1700
|
54
54
|
[to_id] => 1700
|
55
55
|
[from_id] => 1600
|
56
|
+
[children] => Array(
|
57
|
+
[list] => 1600,1700,1800
|
58
|
+
[to_id] => 1800
|
59
|
+
[from_id] => 1700
|
60
|
+
[children] => Array(
|
61
|
+
[list] => 1600,1700,1800.1900
|
62
|
+
[to_id] => 1900
|
63
|
+
[from_id] => 1800
|
56
|
-
)
|
64
|
+
)
|
57
|
-
[1800] => Array
|
58
|
-
(
|
59
|
-
[list] => 1600,1700,1800
|
60
|
-
[to_id] => 1800
|
61
|
-
[from_id] => 1700
|
62
|
-
)
|
65
|
+
)
|
63
|
-
[1900] => Array
|
64
|
-
(
|
65
|
-
[list] => 1600,1700,1800.1900
|
66
|
-
[to_id] => 1900
|
67
|
-
[from_id] => 1800
|
68
66
|
)
|
69
67
|
```
|
70
68
|
|