Re: RecursiveArrayIterator

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

 



the purpose of an iterator is to allow client code to access the various
(aggregate) components of an object while concealing its underlying
implementation.
the client code only has to know about the iterators interface.  thus 1 or
more objects that all have a potentially different data profile internally
can expose something like a getIterator() method.  then client code can
interact cleanly with all such objects, because it knows only 1 interface,
the iterator interface.

http://en.wikipedia.org/wiki/Iterator

-nathan

On 7/23/07, Kevin Waterson <kevin@xxxxxxxxxxx> wrote:

This one time, at band camp, Jim Lucas <lists@xxxxxxxxx> wrote:


> I don't get it, why not do this?

> foreach ( $array AS $row ) {
>       foreach ( $row AS $k => $v ) {
>               if ( ! is_array($v) ) {
>                       echo "{$k} -- {$v}<br/>\n";
>               }
>       }
> }

>
> Maybe I am missing the point...  ???

Indeed, whilst your method is simplistic it leaves many copies of the
array
dangling in memory. Every time you call foreach it creates a copy of the
array internally. SPL iterators do things differently and know only one
element at a time. More can be seen at
http://phpro.org/tutorials/Introduction-to-SPL.html

so the final solution was using a foreach, however, using a foreach on an
SPL iterator object implicitly calls the inner iterator giving us all the
SPL goodness in memory savings.



Kind regards
Kevin



--
"Democracy is two wolves and a lamb voting on what to have for lunch.
Liberty is a well-armed lamb contesting the vote."

--
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