How to Get Minimum Key Value of Array in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Sometimes, you need to get minimum 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 smallest key value from our php array by using min() and array_keys(). So, let's try to use in your code this way:

index.php

<!DOCTYPE html>

<html>

<body>

<?php

$myArray = ['2'=>'Hey','4'=>'Hello','1'=>'Hi','3'=>'GH'];

$new_key = min(array_keys($myArray));

$new_val = $myArray[$new_key];

print_r($new_val);

?>

</body>

</html>

Output:

Hi

I hope it can help you...

Tags :
Shares