note 89129 added to function.iterator-count
| From: | donovanjimenez at osu1 dot php dot net | Date: | Mon, 23 Feb 2009 17:34:37 +0000 |
| Subject: | note 89129 added to function.iterator-count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151023@lists.php.net to get a copy of this message | ||
Be careful of thinking of iterators and arrays as completely analogous in your PHP code.
iterator_count will NOT return your iterator to its previous state after looping through it for the
count. Any iterator implementation that also implements Countable::count isn't required to do
so either.
This is clearest in example form:
<?php
$array = array(
1 => 'foo',
2 => 'bar'
);
foreach ($array as $key => $value)
{
echo "$key: $value (", count($array), ")\n";
}
$iterator = new ArrayIterator($array);
foreach ($iterator as $key => $value)
{
echo "$key: $value (", iterator_count($iterator), ")\n";
}
?>
outputs:
1: foo (2)
2: bar (2)
1: foo (2)
Notice that because of how iterator_count works we never see the second iterator value because the
next call to then Iterator::valid() implementation returns false (its at the end of the iterator).
----
Server IP: 209.41.74.194
Probable Submitter: 216.169.171.3
----
Manual Page -- http://www.php.net/manual/en/function.iterator-count.php
Edit -- https://master.php.net/note/edit/89129
Del: integrated -- https://master.php.net/note/delete/89129/integrated
Del: useless -- https://master.php.net/note/delete/89129/useless
Del: bad code -- https://master.php.net/note/delete/89129/bad+code
Del: spam -- https://master.php.net/note/delete/89129/spam
Del: non-english -- https://master.php.net/note/delete/89129/non-english
Del: in docs -- https://master.php.net/note/delete/89129/in+docs
Del: other reasons-- https://master.php.net/note/delete/89129
Reject -- https://master.php.net/note/reject/89129
Search -- https://master.php.net/manage/user-notes.php