ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> php-general
php-general
Re: [PHP] foreach / unset
by Jochem Maas other posts by this author
Oct 28 2005 6:48AM messages near this date
Re: [PHP] foreach / unset | Re: [PHP] foreach / unset
John Nichel wrote:
>  Richard Lynch wrote:
>  
> > Somewhere in the manual (damned if I can find it now) it says (or used
> > to say) that you can or can't safely do this:
> >
> > while (list($k, $v) = each($array)){
> >   if (...) unset($array[$k]);
> > }
> >
> > I don't even remember if it's safe or not, but I swear I saw it not
> > that long ago...
> >
> > Anyway, can you do *this* safely as a DOCUMENTED FEATURE:
> >
> > foreach($array as $k => $v){
> >   if (...) unset($array[$k]);
> > }
> >
> > I'm sure I could test it and maybe find out if "it works" but is it
> > documented behaviour I can rely on?  I'm sure not finding this in the
> > manual now that I go looking for it, though I know I saw it there
> > before.
>  
>  
>  I would *think* (just my opinion without much thought on a Friday 
>  morning) that this would/could be unsafe _if_ it was a for loop on a 
>  numerical indexed array.  Of course, my thinking may change after my 
>  first bottle of Dew. ;)

this function might change your mind (I take it you have had your Dew by now :-):

     function resolveArgs($args)
     {
         $args = array_values($args);

         for ($i = 0; $i < count($args); ++$i) {
             while (isset($args[$i]) && is_array($args[$i])) {
                 array_splice($args,$i,1,array_values($args[$i]));
             }
         }

         return $args;
     }

basically it flattens a multidimensional array - i use it for handling DB query
arguments (makes it easier to pass around args whilst build highly dynamic queries)

so intrinsically its not unsafe to manipulate the array - only you have the potential
to shoot yourserlf in the foot :-) then again both Richard and John have more than enough
skills to do that anyway ;-)

>  
> > PS
> > I'm being dragged kicking and screaming into using this new-fangled
> > 'foreach' thing instead of while/list/each, and I don't really care
> > for it so far. :-)

new-fangled is putting the boat out a bit thought :-) it's been around since 4.0,
I guess your looking forward to being able to use foreach to iterate over php5 object ;-)

>  
>  
>  For the longest time, I hated foreach.  Looked at other people's code 
>  who used it, and just wanted to strangle them.  However, I was sorta 
>  forced into it about 8 months ago, and now I'm in the camp of, "Damn, I 
>  really like this".

:-)

>  

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Thread:
Richard Lynch
Bogdan Ribic
John Nichel
Jochem Maas
Richard Lynch
Brent Baisley
Jochem Maas
Niels Ganser
Karlos Zafra

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved