質問編集履歴

1

現状のPHPを追加

2019/06/28 11:04

投稿

yutaIMD
yutaIMD

スコア60

test CHANGED
File without changes
test CHANGED
@@ -89,3 +89,109 @@
89
89
  - File03
90
90
 
91
91
  ```
92
+
93
+
94
+
95
+
96
+
97
+ 現状のPHP
98
+
99
+ ```PHP
100
+
101
+ <?php
102
+
103
+ //variables
104
+
105
+ $file = 'list.csv';
106
+
107
+ $all_rows = [];
108
+
109
+ $new = [];
110
+
111
+ $tofind = [];
112
+
113
+ $header = null;
114
+
115
+
116
+
117
+ //function
118
+
119
+ function createTree(&$list, $parent){
120
+
121
+ $tree = array();
122
+
123
+ foreach ($parent as $k=>$l){
124
+
125
+ if(isset($list[$l['code']])){
126
+
127
+ $l['children'] = createTree($list, $list[$l['code']]);
128
+
129
+ }
130
+
131
+ $tree[] = $l;
132
+
133
+ }
134
+
135
+ return $tree;
136
+
137
+ }
138
+
139
+
140
+
141
+
142
+
143
+ //parsing CSV
144
+
145
+ $f = fopen($file, "r");
146
+
147
+ $size = filesize($file) + 1;
148
+
149
+ while ($row = fgetcsv($f, $size, ",")) {
150
+
151
+ if ($header === null) {
152
+
153
+ $header = $row;
154
+
155
+ continue;
156
+
157
+ }
158
+
159
+ $all_rows[] = array_combine($header, $row);
160
+
161
+ }
162
+
163
+
164
+
165
+
166
+
167
+ foreach ($all_rows as $a){
168
+
169
+ $new[$a['parent']][] = $a;
170
+
171
+ }
172
+
173
+ //Get all root parent id
174
+
175
+ $keys = array_keys(array_column($all_rows, 'parent'), '');
176
+
177
+
178
+
179
+ foreach ($keys as $key){
180
+
181
+ $tofind[]= $all_rows[$key];
182
+
183
+ }
184
+
185
+
186
+
187
+ print_r($tofind);
188
+
189
+
190
+
191
+ //Tree
192
+
193
+ $tree = createTree($new, $tofind);
194
+
195
+ ?>
196
+
197
+ ```