find the largest key in a map in php

To find the largest key in a PHP map or associative array, you can use the max() function with the array_keys() function as follows:

main.php
$map = array(
    10 => 'value1',
    20 => 'value2',
    5 => 'value3',
    30 => 'value4'
);

$max_key = max(array_keys($map));
echo $max_key;
145 chars
10 lines

Output:

main.php
30
3 chars
2 lines

Here, the array_keys() function is used to retrieve all the keys in the map, which is passed as an argument to the max() function. The max() function then returns the largest key in the map, which is then stored in the $max_key variable and printed.

gistlibby LogSnag