RE: [PHP] How do I get the first element's key of an array of objects?
| From: | Chris W. Parker | Date: | Thu, 07 Apr 2005 20:18:51 +0000 |
| Subject: | RE: [PHP] How do I get the first element's key of an array of objects? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-212588@lists.php.net to get a copy of this message | ||
Daevid Vincent <mailto:daevid@daevid.com>
on Thursday, April 07, 2005 1:13 PM said:
> I have an array of objects in PHP5. Each element's key is the
> database row ID.
>
> I don't see a graceful way of finding the first element's key ID.
[snip/]
> My hack is to do this:
>
> foreach($fooObject as $key => $object)
> {
> $id = $key;
> break;
> }
>
> There has to be a better way. Something more elegant?
While looking through the array functions I saw www.php.net/array_keys
and thought of:
<?php
$keys = array_keys($object_array);
$first_key = $keys[0];
?>
HTH,
Chris.