Stel, ik heb de volgende dir tree:
Ik probeer nu een functie te maken waarmee ik deze tree in een array kan weergeven:
Op het moment heb ik de volgende functie:
Het gaat fout in regel x. Ik roep klaarblijkelijk dir_tree() niet correct aan om bovenstaande array te krijgen. Ik heb al verschillende oplossingen geprobeerd, zoals
Kan iemand mij zeggen wat ik fout doe?
code:
1
2
3
4
5
6
7
| images system editor templates user uploads vrijdag |
Ik probeer nu een functie te maken waarmee ik deze tree in een array kan weergeven:
code:
1
2
3
4
5
6
7
8
9
| Array ( [images] => images/ [system] => images/system/ [editor] => images/system/editor/ [templates] => images/system/templates/ [user] => images/user/ [uploads] => images/user/uploads/ [uploads] => images/user/uploads/vrijdag/ ) |
Op het moment heb ik de volgende functie:
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| function dir_tree($dir) { $handle = opendir($dir); while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') { $path = $dir.$file; if (is_dir($path)) { $output[$file] = $path; dir_tree($path); } } } closedir($handle); return isset($output) ? $output : false; } |
Het gaat fout in regel x. Ik roep klaarblijkelijk dir_tree() niet correct aan om bovenstaande array te krijgen. Ik heb al verschillende oplossingen geprobeerd, zoals
PHP:
Deze brengen alleen een array als output die nog veel verder van huis ligt dan de originele functie. Ook heb ik op PHP.net gezocht naar een oplossing, maar daar worden alleen maar functies gegeven die array's terugggeven die zelf ook meerdere niveau's hebben:1
2
3
4
5
6
| $output[$file] = $path; $output[$file] = dir_tree($path); // en $output[] = array_merge(dir_tree($path), $output); |
PHP:
Maar dat heb ik dus juist niet nodig 1
| $output[$file] = dir_tree($path); |
"Real software engineers work from 9 to 5, because that is the way the job is described in the formal spec. Working late would feel like using an undocumented external procedure."