note 41636 added to ref.spl
| From: | phil&ersat at rn2 dot php dot net | Date: | Sat, 17 Apr 2004 19:11:37 +0000 |
| Subject: | note 41636 added to ref.spl | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-68257@lists.php.net to get a copy of this message | ||
Note that there have been changes to the SPL iterator method names since this page was first
published. Iterator::hasMore() is now Iterator::valid();
Example ArrayIterator usage:
<?php
$my_array = array(
'One' => 'First Item',
'Two' => 'Second Item',
'Three' => 'Third Item'
);
$ArrayIterator = new ArrayObject($my_array);
for($it = $ArrayIterator->getIterator(); $it->valid(); $it->next()) {
printf('%s => $s <br />', $it->key(), $it->current());
}
?>
Here's a sample implementation of the RecursiveDirectoryIterator class. It prints a simple
treeview of a given directory:
<?php
function recurse($it) {
echo '<ul>';
for( ; $it->valid(); $it->next()) {
if($it->isDir() && !$it->isDot()) {
printf('<li class="dir">%s</li>', $it->current());
if($it->hasChildren()) {
$bleh = $it->getChildren();
printf('<ul>%s</ul>');
echo '<ul>' . recurse($bleh) . '</ul>';
}
} elseif($it->isFile()) {
printf('<li class="file">%s (%u Bytes)</li>',
$it->current(), $it->getSize());
}
}
echo '</ul>';
}
recurse(new RecursiveDirectoryIterator('D:/'));
?>
----
Manual Page -- http://www.php.net/manual/en/ref.spl.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41636
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41636&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41636&report=yes
Search -- http://master.php.net/manage/user-notes.php