NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing First Element of Array

Ken Downs ken at secdat.com
Mon Aug 27 07:45:50 EDT 2007


"Michael B Allen" <ioplex at gmail.com> wrote: > > >
> > > Um, you could, uh, write a function called array_first() that
does the > > reset() and then calls each()?  Then maybe even calls
reset() again?  Then > > you could, like, maybe, only use this function
and not make arbitrary use of > > each() and reset() in your code? > 
> I guess you missed my earlier post. This is what I'm using right now:
>  > function _array_first($a, $want_key=FALSE) { >     foreach ($a
as $key => $elem) >         return $want_key ? $key : $elem; >    
return FALSE; > } >  > > And btw, when you write that function,
pass the array by reference, not by > > value.No, I caught your post,
it's just that an experienced PHP programmer would never write the function
you wrote to do what you're doing.  They'd use each().Hint: read the
manual to find find out why I told you to pass by reference, read the manual
to find out what foreach does internally.Have a nice day.--Ken DownsSecure
Data Softwarewww.secdat.com631-689-7200631-379-0010
_________________________________________________________
This mail sent using V-webmail - http://www.v-webmail.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20070827/0a1a7b6a/attachment.html>


More information about the talk mailing list