find the range of all keys in a map in php

To find the range (minimum and maximum values) of all the keys in a PHP map (associative array), you can use the min and max functions with the array_keys function. Here's an example code snippet:

main.php
$map = array(
    'apple' => 3,
    'banana' => 1,
    'orange' => 2
);

$keys = array_keys($map);

$min_key = min($keys);
$max_key = max($keys);

echo "Minimum key: " . $min_key . "\n";  // Output: Minimum key: apple
echo "Maximum key: " . $max_key . "\n";  // Output: Maximum key: orange
290 chars
14 lines

In this example, we first create a map (associative array) with some values. Then, we use the array_keys function to get an array of all the keys in the map. Finally, we use the min and max functions to find the minimum and maximum keys in the keys array.

Note that the min and max functions work with strings as well as numbers, so they can be used to find the range of any keys in the map.

related categories

gistlibby LogSnag