gistlib
To find the kth largest key in a map in PHP, you can follow these steps:
array_keys
rsort
Here's the PHP code:
main.phpfunction findKthLargestKey($map, $k) { $keys = array_keys($map); rsort($keys); return $keys[$k-1]; } 113 chars6 lines
function findKthLargestKey($map, $k) { $keys = array_keys($map); rsort($keys); return $keys[$k-1]; }
You can use this function like this:
main.php$map = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $k = 2; // find the 2nd largest key $kthLargestKey = findKthLargestKey($map, $k); echo $kthLargestKey; // output: 'c' 172 chars5 lines
$map = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4); $k = 2; // find the 2nd largest key $kthLargestKey = findKthLargestKey($map, $k); echo $kthLargestKey; // output: 'c'
gistlibby LogSnag