Re: spl DirectoryIterator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Matthew Dellar wrote:
> I have a problem,
> 
> I need to turn an iterator into an array, but when I do, some methods I
> need to use stop working.

why do you need to turn it into an array? maybe there is an alternative?
my first reaction would be that if you really need an array then you'll
probably be required to write your own conversion function, e.g.

function myI2A(DirectoryIterator $i)
{
	$a = array();
	foreach ($i as $el) {
		$a[] = array(
			'file' => $el->getFileName(),
			'path' => $el->getPath(),
			// etc
		);
	}
	return $a;
}

> 
> Take a look at the following example:
> 
> $dir = 'c:/';
> $files = new DirectoryIterator($dir);
> //$files = iterator_to_array($files);
> foreach ($files as $file) {

$file is, AFAIC tell, a reference to the DirectoryIterator object ...
the methods $file exposes are methods of the DirectoryIterator object
that return values in the context of the current position in the iterator.

this thinking is based on the following docs:

	http://www.php.net/~helly/php/ext/spl/classDirectoryIterator.html

and the stub-docs at php.net.

>     echo "{$file->getFileName()}<br>";//works
>     echo "{$file->getPath()}<br>";//works
> }
> 
> It works as expected. However, when the iterator is turned into an array:
> 
> $dir = 'c:/';
> $files = new DirectoryIterator($dir);
> $files = iterator_to_array($files);

given what I said above (and assuming it is correct) this would explain
why your getting back an array of DirectoryIterator objects.

my guess is that iterator_to_array() can't currently handle all kinds of
iterator (especially more exotic implementations)

> foreach ($files as $file) {
>     echo "{$file->getFileName()}<br>"; //does not work
>     echo "{$file->getPath()}<br>";//works
> }
> 
> It stops working. Can someone please help me, as a have tried and failed
> to find the cause of the problem.
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux