note 59539 added to function.array-keys
| From: | vesely at tana dot it | Date: | Fri, 09 Dec 2005 09:56:35 +0000 |
| Subject: | note 59539 added to function.array-keys | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99806@lists.php.net to get a copy of this message | ||
The position of an element.
One can apply array_keys twice to get the position of an element from its key. (This is the reverse
of the function by cristianDOTzuddas.) E.g., the following may output "yes, we have bananas at
position 0".
<?php
$a = array("banana" => "yellow", "apple" = "red");
$k = get_some_fruit();
if (isset($a[$k]))
{
list($pos) = array_keys(array_keys($a), $k);
print "yes, we have {$k}s at position $pos\n";
}
?>
Not amazingly efficient, but I see no better alternative.
----
Manual Page -- http://www.php.net/manual/en/function.array-keys.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59539
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59539&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59539&report=yes
Search -- http://master.php.net/manage/user-notes.php