Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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.

like image 940
Mark Buskbjerg Avatar asked Mar 13 '23 06:03

Mark Buskbjerg


2 Answers

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]
like image 116
splash58 Avatar answered Mar 15 '23 03:03

splash58


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

like image 30
Murlidhar Fichadia Avatar answered Mar 15 '23 03:03

Murlidhar Fichadia