Re: Getting key of value added to array with []?
| From: | Goetz Lohmann | Date: | Tue, 04 Feb 2003 12:05:37 +0000 |
| Subject: | Re: Getting key of value added to array with []? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134250@lists.php.net to get a copy of this message | ||
Leif K-Brooks schrieb:
> Is there any way to get the key of an element added to an array with []?
> I need to use this for a reference to it. The only idea I can think of
> is to foreach through the array and use the last key looped through, but
> that seems very dirty.
>
$foo[]='bar';
will insert 'bar' as the last item of the array $foo. To get this you might
use
$erg = key($foo); // the key of the current element
or
$erg = pos($foo); // the value of the current element
maybe if something happend between the insertion first head to the last item
$erg = end($foo); // get the last element of array $foo
instead of foreach you can also use array_walk($arr, 'func');
--
@ Goetz Lohmann, Germany | Web-Developer & Sys-Admin
\/ ------------------------------------------------------
() He's the fellow that people wonder what he does and
|| why the company needs him, until he goes on vacation.