Home php How to get maximum key value of array in PHP?

How to get maximum key value of array in PHP?

by Shahriar Sagor

Today now in this post i will show you how to get maximum key value of array in php. Sometimes, we need to get maximum key value of our array but we try to get with loop and try any function etc, but now in bellow example we can see i can get biggest key value from our php array by using max() and array_keys(). So, let’s start and try to use in our code this way:

Example:

$myArray = ['2'=>'Hey','4'=>'Hello','1'=>'Hi','3'=>'GH'];
$new_key = max(array_keys($myArray));
$new_val = $myArray[$new_key];
print_r($new_val);

Read Also : Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us on facebook.

close

You may also like