How to Get Maximum Key Value of Array in PHP?
Sometimes, you need to get maximum key value of your array but you try to get with loop and any function etc, but in bellow example you can see we can get biggest key value from our php array by using max() and array_keys(). So, let's try to use in your code this way:
Example:
<?php
$myArray = ['2'=>'Hey','4'=>'Hello','1'=>'Hi','3'=>'GH'];
$new_key = max(array_keys($myArray));
$new_val = $myArray[$new_key];
print_r($new_val);
?>
Output:
Hello
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Check Current PHP Version in Ubuntu?
- How to Write PHP Code in Laravel Blade?
- Convert HTML to PDF in PHP with Dompdf Example
- How to Get Folder Path from File Path in PHP?
- How to Send Mail in PHP Laravel?
- How to Convert Object into Array in PHP?
- How to Count Number of Files in Directory using PHP?
- How to access PHP variables in JQuery?
- How to Count Number of Files in a Directory in PHP?
- How to Find Day Name from Specific Date in PHP?
- How to Remove Null Values from Array in PHP?
- How to Merge Two Array with Same Keys without Loop in PHP?