note 110570 added to function.scandir
| From: | mmda dot nl at gmail dot com | Date: | Thu, 08 Nov 2012 12:43:19 +0000 |
| Subject: | note 110570 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-191808@lists.php.net to get a copy of this message | ||
Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to
easely access data in a certain directory using foreach. I came up with the following:
function dirToArray($dir) {
$result = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$result[] = $value;
}
}
}
return $result;
}
Output
Array
(
[subdir1] => Array
(
[0] => file1.txt
[subsubdir] => Array
(
[0] => file2.txt
[1] => file3.txt
)
)
[subdir2] => Array
(
[0] => file4.txt
}
)
----
Server IP: 92.48.119.85
Probable Submitter: 46.44.132.100
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- https://master.php.net/note/edit/110570
Del: integrated -- https://master.php.net/note/delete/110570/integrated
Del: useless -- https://master.php.net/note/delete/110570/useless
Del: bad code -- https://master.php.net/note/delete/110570/bad+code
Del: spam -- https://master.php.net/note/delete/110570/spam
Del: non-english -- https://master.php.net/note/delete/110570/non-english
Del: in docs -- https://master.php.net/note/delete/110570/in+docs
Del: other reasons-- https://master.php.net/note/delete/110570
Reject -- https://master.php.net/note/reject/110570
Search -- https://master.php.net/manage/user-notes.php