[英]Files to array - glob vs RecursiveIteratorIterator
I'm adding a list of files to an array by doing: 我通过执行以下操作将文件列表添加到数组中:
$files = glob($path."*.*");
I can output the array with print_r(array_values($array));
我可以用
print_r(array_values($array));
输出数组print_r(array_values($array));
. 。
Now I'm trying to do the same with RecursiveIteratorIterator
: 现在,我尝试使用
RecursiveIteratorIterator
进行相同操作:
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path)
);
That does not work like expected when I try to output the array? 当我尝试输出数组时,这不能按预期工作?
print_r(array_values($files));
How can I get the same result? 如何获得相同的结果?
Iterators are not the same as arrays. 迭代器与数组不同。 To use an iterator, you have to iterate over it with
foreach
. 要使用迭代器,您必须使用
foreach
对其进行迭代。
<?php
$path = "test.dir";
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($path)
);
$file_array = array();
foreach ($files as $f) {
$file_array[] = $f;
}
print_r($file_array);
Output: 输出:
Array
(
[0] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/.
[fileName:SplFileInfo:private] => .
)
[1] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/..
[fileName:SplFileInfo:private] => ..
)
[2] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub1/.
[fileName:SplFileInfo:private] => .
)
[3] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub1/..
[fileName:SplFileInfo:private] => ..
)
[4] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub1/filea.jpg
[fileName:SplFileInfo:private] => filea.jpg
)
[5] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub1/fileb.jpg
[fileName:SplFileInfo:private] => fileb.jpg
)
[6] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub2/.
[fileName:SplFileInfo:private] => .
)
[7] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub2/..
[fileName:SplFileInfo:private] => ..
)
[8] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub2/sub21/.
[fileName:SplFileInfo:private] => .
)
[9] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub2/sub21/..
[fileName:SplFileInfo:private] => ..
)
[10] => SplFileInfo Object
(
[pathName:SplFileInfo:private] => test.dir/sub2/sub21/filee.jpg
[fileName:SplFileInfo:private] => filee.jpg
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.