Longhorn PHP 2025 - Call For Papers

Voting

: six minus two?
(Example: nine)

The Note You're Voting On

vesely at tana dot it
19 years ago
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.

<< Back to user notes page

To Top