Reputation: 2533
I have a specific question. I'm new on php so it still gets a bit tough for me :)
So, I have a function which shoud scan dirs recursivly and print a specific array from result. I almost understand why it is happening, but I could not figure out how to change it like I need to. Please help!
At the moment I'm getting an array:
Array
(
[0] => Array
(
[album1] => Array
(
[0] => .
[1] => ..
)
)
[1] => Array
(
[album2] => Array
(
[0] => .
[1] => ..
)
)
[2] => Array
(
[album3] => Array
(
[0] => .
[1] => ..
)
)
)
But I need to get:
Array
(
[album1] => Array (
[0] => .
[1] => ..
)
[album2] => Array
(
[0] => .
[1] => ..
)
[album3] => Array
(
[0] => .
[1] => ..
)
)
The function itself is:
<?php
function RAgetFiles($main_dir, $result = array()) {
$dirs = scandir($main_dir);
foreach($dirs as $dir) {
if (is_dir("$main_dir/$dir")){
if ($dir === '.' || $dir === '..') {
continue; }
$files=scandir($main_dir."/".$dir);
$result[] = array($dir => $files);
}
}
return $result;
}
$rafiles = RAgetFiles('thumbs');
echo '<pre>';
print_r($rafiles);
echo '<pre>';
?>
Upvotes: 0
Views: 419
Reputation: 642
<?php
function RAgetFiles($main_dir, $result = array()) {
$dirs = scandir($main_dir);
foreach($dirs as $dir) {
if (is_dir("$main_dir/$dir")){
if ($dir === '.' || $dir === '..') {
continue; }
$files=scandir($main_dir."/".$dir);
$result[$dir] = $files;
}
}
return $result;
}
$rafiles = RAgetFiles('/usr');
echo '<pre>';
print_r($rafiles);
echo '</pre>';
?>
Works perfectly.
Upvotes: 1