RE: [PHP] Converting array keys to variables?
| From: | Ford, Mike | Date: | Tue, 21 Nov 2006 11:18:02 +0000 |
| Subject: | RE: [PHP] Converting array keys to variables? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-244944@lists.php.net to get a copy of this message | ||
On 20 November 2006 22:55, Richard Lynch wrote:
> On Fri, November 17, 2006 10:23 am, Ashley M. Kirchner wrote:
> > [hours] => Array
> > (
> > [0] => Array
> > (
> > [file] => capture.0400.jpg
> > [path] => spool/.2006/11/17/04
> > [year] => 2006
> > [month] => 11
> > [day] => 17
> > [hhmm] => 0400
> > )
> ...
> > Is there a way that I can simply loop through each array and
> > convert the keys into variables? I want to avoid having to write
> > lines of:
>
> foreach($array['hours'] as $key => $image_meta){
> list($file, $path, $year, $month, $day, $hhmm) = $image_meta;
> //Do whatever you want with the variables.
> }
>
> I personally would avoid 'extract' function, as a matter of principle,
> as it's to easy to add something to an array and not realize it, and
> then you start over-writing variables you didn't mean to, because your
> key name matches some other variable you didn't mean to over-write...
That's what the extract_type and prefix parameters are for.
Although I wouldn't use extract() either, as I'd just address the array elements directly
using a full set of subscripts.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm