Re: array key's: which is correct?
| From: | Ashley Sheridan | Date: | Tue, 08 Jun 2010 13:57:42 +0000 |
| Subject: | Re: array key's: which is correct? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-305954@lists.php.net to get a copy of this message | ||
On Tue, 2010-06-08 at 09:38 -0400, Robert Cummings wrote:
> Tanel Tammik wrote:
> > Hi,
> >
> > which one is correct or "better"?
> >
> > $array[3] = '';
> > or
> > $array['3'] = '';
> >
> > $i = 7;
> >
> > $array[$i] = '';
> > or
> > $array["$i"] = '';
>
> Sometimes it is good to illustrate the correct answer:
>
> <?php
>
> $array = array
> (
> '1' => '1',
> '2' => '2',
> 'three' => 'three',
> '4.0' => '4.0',
> 5.0 => 5.0,
> );
>
> var_dump( array_keys( $array ) );
>
> ?>
>
> The answer is surprising (well, not really :) and certainly advocates
> against making literal strings of integers or manually converting a
> string integer to a real integer or using floating point keys.
>
> Cheers,
> Rob.
> --
> E-Mail Disclaimer: Information contained in this message and any
> attached documents is considered confidential and legally protected.
> This message is intended solely for the addressee(s). Disclosure,
> copying, and distribution are prohibited unless authorized.
>
Yeah, I found that out the hard way when I was trying to make an array
of Gantt tasks, and realised that all my nice task numbers were changed!
Thanks,
Ash
http://www.ashleysheridan.co.uk