PHP Array Exercises : Get the largest key in an array
PHP Array: Exercise-16 with Solution
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php-exercises/php-array-exercise-16.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics