This is a discussion on RE: [PHP] help with arrays within the PHP General forums, part of the PHP Programming Forums category; Robert Cummings <mailto:robert@interjinn.com> on Tuesday, September 23, 2003 1:43 PM said: > It doesn'...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Robert Cummings <mailto:robert@interjinn.com>
on Tuesday, September 23, 2003 1:43 PM said: > It doesn't get much simpler :) Doh!! > foreach( $data as $key => $value ) I don't use that syntax much so I'm not familiar with it. Maybe I should start eh? Thanks, Chris. |
|
|||
|
On Tue, 2003-09-23 at 17:37, Eugene Lee wrote:
> On Tue, Sep 23, 2003 at 01:49:03PM -0700, Chris W. Parker wrote: > : > : Robert Cummings <mailto:robert@interjinn.com> said: > : > > : > foreach( $data as $key => $value ) > : > : I don't use that syntax much so I'm not familiar with it. Maybe I should > : start eh? > > Another caveat: it works on a *copy* of the array. This is fine for > small arrays, but may be a performance killer for larger arrays. I don't think you have enough knowledge of the PHP internals to make that claim :) To qualify my statement, PHP doesn't not literally copy data until you attempt to make a change. So in the above example $key and $value are virtual copies of relevant data. If I were to try and change either of these values then an explicit copy would be made at that time, but not before. Thus since I actually make modifications on the original $data array structure, my example makes no real copies except when the new values is set in the original array. My example is much more optimal than you think, mostly because of the oft unknown optimizations made in the PHP engine itself. Cheers, Rob. -- ..------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' |
|
|||
|
On Tue, Sep 23, 2003 at 05:44:46PM -0400, Robert Cummings wrote:
: On Tue, 2003-09-23 at 17:37, Eugene Lee wrote: : > On Tue, Sep 23, 2003 at 01:49:03PM -0700, Chris W. Parker wrote: : > : : > : Robert Cummings <mailto:robert@interjinn.com> said: : > : > : > : > foreach( $data as $key => $value ) : > : : > : I don't use that syntax much so I'm not familiar with it. Maybe I should : > : start eh? : > : > Another caveat: it works on a *copy* of the array. This is fine for : > small arrays, but may be a performance killer for larger arrays. : : I don't think you have enough knowledge of the PHP internals to make : that claim :) You're right. I know little of PHP internals. I'm just going by what little the official documentation tells me. :-) http://www.php.net/manual/en/control...es.foreach.php Note: Also note that foreach operates on a copy of the specified array and not the array itself. |