0

Halo people.

There is possible to select select min (lowest) or max (bigest) from array float number?

Im try min(array) and max(array) but not working? I can not found on manual.

Can you help me?

The array comes from sql

Array ( [0] => 1.11954 ) Array ( [0] => 1.11983 ) Array ( [0] => 1.11854 ) Array ( [0] => 1.11978 ) Array ( [0] => 1.1198 ) Array ( [0] => 1.12024 ) Array ( [0] => 1.11994 ) Array ( [0] => 1.12055 ) Array ( [0] => 1.12106 ) Array ( [0] => 1.12186 ) Array ( [0] => 1.12191 ) Array ( [0] => 1.1214 ) Array ( [0] => 1.12432 ) Array ( [0] => 1.12398 )
 for ($list = 1; $list <= $rezult; $list++)
    {
     $_array=array($rekord['xxx'])
    }
print_r($_array);
$_min=min($_array);
$_max=max($_array);
Miroslav Glamuzina
  • 4,472
  • 2
  • 19
  • 33
pacanosiu
  • 37
  • 11

1 Answers1

3

Your problem is that your array is an array of arrays, not an array of floating point numbers, so to find the min/max values you effectively need to flatten the array, which you can do with array_column:

$flat = array_column($array, 0);
echo min($flat), " ", max($flat);

Output:

1.11854 1.12432

Demo on 3v4l.org

Alternatively you can recode your loop to push values, rather than arrays into it:

for ($list = 1; $list <= $rezult; $list++) {
     $_array[] = $rekord['xxx']
}
echo min($_array) . " " . max($_array);
Nick
  • 138,499
  • 22
  • 57
  • 95
  • @dervil, yes, have just looked at your code more closely and you are overwriting the array each time through the loop instead of pushing new values into it. I think what you really wanted to do was the code that I added in the edit I just made. – Nick Mar 09 '19 at 00:39
  • yes i forget obout this [] , and after added must use array_column not just array, thank you – pacanosiu Mar 09 '19 at 00:49
  • @mickmackusa cleaned up. – Nick May 04 '20 at 12:31