PHP Array Exercises : Get the largest key in an array
Write a PHP script to get the largest key in an array.
Sample Solution:
PHP Code:
<?php
// Define an associative array $ceu with countries as keys and their capitals as values
$ceu = array(
"Italy" => "Rome",
"Luxembourg" => "Luxembourg",
"Belgium" => "Brussels",
"Denmark" => "Copenhagen",
"Finland" => "Helsinki",
"France" => "Paris",
"Slovakia" => "Bratislava",
"Slovenia" => "Ljubljana",
"Germany" => "Berlin",
"Greece" => "Athens",
"Ireland" => "Dublin",
"Netherlands" => "Amsterdam",
"Portugal" => "Lisbon",
"Spain" => "Madrid",
"Sweden" => "Stockholm",
"United Kingdom" => "London",
"Cyprus" => "Nicosia",
"Lithuania" => "Vilnius",
"Czech Republic" => "Prague",
"Estonia" => "Tallin",
"Hungary" => "Budapest",
"Latvia" => "Riga",
"Malta" => "Valetta",
"Austria" => "Vienna",
"Poland" => "Warsaw"
);
// Use 'max' and 'array_keys' functions to find the maximum key in the associative array
$max_key = max(array_keys($ceu));
// Echo the maximum key
echo $max_key . "\n";
?>
Output:
United Kingdom
Flowchart:
PHP Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a PHP script to generate unique random numbers within a range.
Next: Write a PHP function that returns the lowest integer that is not 0.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics