Doc #62914 [Com]: First entry appears twice, unless rewind method is called first
| From: | phpbugs at jessemccarthy dot net | Date: | Sun, 26 Aug 2012 14:52:19 +0000 |
| Subject: | Doc #62914 [Com]: First entry appears twice, unless rewind method is called first | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8768@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62914&edit=1
ID: 62914
Comment by: phpbugs at jessemccarthy dot net
Reported by: lveyde at gmail dot com
Summary: First entry appears twice, unless rewind method is
called first
Status: Open
Type: Documentation Problem
Package: SPL related
Operating System: CentOS 6.3
PHP Version: 5.3.16
Block user comment: N
Private report: N
New Comment:
I'm running into this too, in PHP 5.3.6 (Apache module / CLI) on Ubuntu 11.10.
Test case directory structure:
test/
a/
b.php
iterate.php
while.php
foreach.php
rewind-while.php
iterator_to_array.php
iterate.php is used in all of the other scripts:
<?php
header( "Content-Type: text/plain" );
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
__DIR__ . "/a",
FilesystemIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST
);
function show_items( $items ) {
var_dump( $items );
}
$items = array();
****************************************
Expected result from all scripts:
****************************************
array(1) {
[0]=>
string(5) "b.php"
}
Content of the test scripts and actual results:
****************************************
while.php
****************************************
<?php
require_once "iterate.php";
while ( $it->valid() ) {
$items[] = $it->getFilename();
$it->next();
}
show_items( $items );
========================================
array(2) {
[0]=>
string(5) "b.php"
[1]=>
string(5) "b.php"
}
****************************************
foreach.php
****************************************
<?php
require_once "iterate.php";
foreach ( $it as $file ) {
$items[] = $file->getFilename();
}
show_items( $items );
========================================
array(1) {
[0]=>
string(5) "b.php"
}
****************************************
rewind-while.php
****************************************
<?php
require_once "iterate.php";
$it->rewind();
require_once "while.php";
========================================
array(1) {
[0]=>
string(5) "b.php"
}
****************************************
iterator_to_array.php
****************************************
<?php
require_once "iterate.php";
$items = array_map( function ( $item ) {
return $item->getFilename();
}, array_values( iterator_to_array( $it ) ) );
show_items( $items );
========================================
array(1) {
[0]=>
string(5) "b.php"
}
As you can see, iterating with
while and next() produces an unexpected
duplicate value, whereas foreach, iterator_to_array(), and the
rewind()
then while suggested by the OP produce the expected result.
Previous Comments:
------------------------------------------------------------------------
[2012-08-23 22:18:04] lveyde at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/recursivedirectoryiterator.construct
---
I tried to run the sample code (changing the directory), and unless the call to the rewind() method
is performed before using the iterator, the first entry appears twice in the output.
I'm using PHP version 5.3.3, the latest available with RHEL/CentOS 6.x.
<?php
$directory = '/root/isos';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
// Added this
$it->rewind();
while($it->valid()) {
if (!$it->isDot()) {
echo 'SubPathName: ' . $it->getSubPathName() . "\n";
echo 'SubPath: ' . $it->getSubPath() . "\n";
echo 'Key: ' . $it->key() . "\n\n";
}
$it->next();
}
?>
Is that a normal behavior (and thus bug in the documentation) ?
Test script:
---------------
<?php
$directory = '/root/isos';
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
while($it->valid()) {
if (!$it->isDot()) {
echo 'SubPathName: ' . $it->getSubPathName() . "\n";
echo 'SubPath: ' . $it->getSubPath() . "\n";
echo 'Key: ' . $it->key() . "\n\n";
}
$it->next();
}
?>
Expected result:
----------------
SubPathName: CentOS-6.2-x86_64-netinstall.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-netinstall.iso
SubPathName: CentOS-6.2-x86_64-bin-DVD1.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-bin-DVD1.iso
SubPathName: CentOS-6.2-x86_64-LiveCD.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-LiveCD.iso
SubPathName: CentOS-6.2-x86_64-bin-DVD2.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-bin-DVD2.iso
Actual result:
--------------
SubPathName: CentOS-6.2-x86_64-netinstall.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-netinstall.iso
SubPathName: CentOS-6.2-x86_64-netinstall.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-netinstall.iso
SubPathName: CentOS-6.2-x86_64-bin-DVD1.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-bin-DVD1.iso
SubPathName: CentOS-6.2-x86_64-LiveCD.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-LiveCD.iso
SubPathName: CentOS-6.2-x86_64-bin-DVD2.iso
SubPath:
Key: /root/isos/CentOS-6.2-x86_64-bin-DVD2.iso
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62914&edit=1