Mark Buskbjerg
Mark Buskbjerg

Reputation: 152

How to know which variable returns lowest values in PHP min()

OK. So I have a bunch of variables containing a number from 1-4. Like:

$score1 = 1;
$score2 = 3;
$score3 = 2;
$score4 = 1;
$score5 = 4;
$score6 = 2;

And then I use

min($score1, $score2, $score3, $score, $score5, $score6);

and the result is that 1 is the lowest score.

Is there a way for me to find out which variables returned the lowest score?

In this particular example this would then tell me that $score1 and $score4 returned an integer equal to the lowest integer.

Any suggestions much appreciated.

Upvotes: 4

Views: 711

Answers (3)

sinisake
sinisake

Reputation: 11328

If you, for some reason, don't want (or can't) to use array:

$min=min($score1, $score2, $score3, $score4, $score5, $score6);

for($i=1;$i<7;$i++) {

    if(${"score".$i}==$min) {
        echo '$score'.$i;

    }

}

Upvotes: 1

splash58
splash58

Reputation: 26153

make array and find keys with values equal to min value

$a = array($score1, $score2, $score3, $score4, $score5, $score6);
print_r(array_keys($a, min($a))); // [0,3]

Upvotes: 5

Murlidhar Fichadia
Murlidhar Fichadia

Reputation: 2609

I would first and foremost recommend using an array rather than sooo many variables. And in an array we have something like array[0], array[1], and so on.

And then once you have all the scores in an array you can use built in methods like get the index of an array or sort an array values or get the min value etc.

The best way to find out the lowest score would be by first sorting in ascending and pulling the first array index value. But there are many approaches in getting the min value and the location of it in an array.

Hope it helps

Upvotes: 1

Related Questions