w3resource
php Programming Exercies

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
$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") ;
$max_key = max( array_keys( $ceu) ); 
echo $max_key."\n";
?>

Sample Output:

United Kingdom    

Flowchart:

Flowchart: Get the largest key in an array

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?



New Content: Composer: Dependency manager for PHP, R Programming