2

I have a short question:

This is my array:

Array ( [1] => 03 [2] => 02 )

I want to print the value of the lowest key. This is my code:

$result = min(array_keys($myarray));

This prints:

1

But what I need is the value. So the result I want is

03

I tried different variations, for example:

foreach ($result as $key => $value) {
    echo $value;
}

But nothing is working. Can you help me?

peace_love
  • 6,229
  • 11
  • 69
  • 157

5 Answers5

2
$myarray = array( 1 => 3, 2 => 2, 3 => 1 );
//Lowest Value
$result = min(array_values($myarray));
var_dump($result);

//Highest Value
$result = max(array_values($myarray));
var_dump($result);

//Answer
var_dump($myarray[min(array_keys($myarray))]);

//Output
int(1)
int(3)
int(3)

I thope this helps :)

geggleto
  • 2,605
  • 1
  • 15
  • 18
  • yes, I was looking for something like `array_values` but couldn't find it. Thanks a lot! =) – peace_love Dec 04 '15 at 20:39
  • Except that this answer doesn't do what you actually asked for, it simply returns the lowest value, and you can do that simply using `min($myArray)` without any need to use `array_values()` – Mark Baker Dec 04 '15 at 20:40
2
$_ar[min(array_keys($arr))]

Hope this will work fine.

int soumen
  • 521
  • 5
  • 14
1

If you are looking for the lowest key's value, you were on the right track!

$key fetches the min key and then you use it in the array to fetch the value:

$key = min(array_keys($myarray));
echo $myarray[$key];
Hasse Björk
  • 1,431
  • 13
  • 19
1

You can sort your array by key using:

Ksort($arr);

Then

echo $arr[0];
Gouda Elalfy
  • 6,888
  • 1
  • 26
  • 38
1

Just do it like this:

$myarray = [15 => 12, 4 => 43, 1 => 45];
$result = $myarray[min(array_keys($myarray))];
echo $result;

Proof: https://3v4l.org/okRFY

jankal
  • 1,090
  • 1
  • 11
  • 28