Charles
Charles

Reputation: 189

PHP get array key for lowest array value

Using PHP, I am creating an array that contains multiple keys:

$loadAverage[$server] = $output["now"];

I am then checking the item with the lowest value

echo min($loadAverage);

but i need to get the key of the array not the value (so the $server part i need)

the above is returning the value and not the key of the PHP array

Upvotes: 0

Views: 109

Answers (3)

RST
RST

Reputation: 3925

$lowest_key = array_keys($loadAverage, min($loadAverage));
echo $lowest_key[0];

By supplying a search element you only get the key of that value.

Upvotes: 1

Charles
Charles

Reputation: 189

echo array_search(min($loadAverage), $loadAverage);

Upvotes: 0

Ben Harold
Ben Harold

Reputation: 6432

echo min(array_keys($loadAverage));

Upvotes: 1

Related Questions