Re: RecursiveArrayIterator

From: Date: Mon, 23 Jul 2007 06:36:24 +0000
Subject: Re: RecursiveArrayIterator
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-259357@lists.php.net to get a copy of this message
This one time, at band camp, Jim Lucas <lists@cmsws.com> 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."

« previous php.general (#259357) next »