Re: [PHP4BETA] no reverse fetch of arrays is BAD!
| From: | Colin Putney | Date: | Wed, 23 Feb 2000 10:01:44 +0000 |
| Subject: | Re: [PHP4BETA] no reverse fetch of arrays is BAD! | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-10709@lists.php.net to get a copy of this message | ||
-- Faisal Nasim <faisal@nasim.org> wrote:
> key ( $x ) returned the lower bound.....
>
> The definition for key():
>
> key -- Fetch a key from an **associative** array
>
> Which caused me to skip it while checking the
> array functions! Can anybody change/fix that?
Well, actually all arrays in PHP are effectively associative arrays.
"Normal" arrays (ie. resembling C arrays) are just associative arrays
that happen to have consecutive integers begining with 0 as keys.
The following creates a perfectly valid array with six values:
$x[14] = 1;
$x[0] = 2;
$x["any string will do as a key"] = 3;
$x[-6] = 4;
$x[""] = 5;
$x[9568] = 6;
It may seem a bit wierd at first - I know it did for me - but this is
definitely a *feature*. each() is a handy function for traversing this
sort of array. If you want to go backwards I suggest array_reverse()
first, then using each(). The other alternative - end()/prev()/key() -
will stumble on the second (probably) and fifth items (definitely) of
the above array.
Enjoy,
Colin
----------------------------------------------
Colin Putney cputney@whistler.net
Whistler Networks http://www.whistler.net/