Forum Moderators: coopster

Message Too Old, No Replies

determining a high and low value in an array of variables?

         

deras

2:29 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



if i have 10 variables (f1, f2, f3... f10) with numerical values, how would i determine the highest numerical value f# and lowest numerical value f# of the 10 using php?

Timotheos

4:32 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the max [php.net] and min [php.net] functions.

deras

11:19 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



i can get the highest and lowest number values that way, but it does not tell me which variable that highest or lowest value came from. how do i determine that?

Timotheos

11:41 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ya just had ta ask ;-) By the looks of it if you can have the variables in an array it should make it easier. This should work with an associative array as well.

<?php
$arSrc[0]=14;
$arSrc[1]=16;
$arSrc[2]=13;
$arSrc[3]=17;

$iMinValue = min($arSrc);
$arFlip = array_flip($arSrc);
$iMinPosition = $arFlip[$iMinValue];

echo
'<br />min_value=',
$iMinValue,
'<br />min_position=',
$iMinPosition
;
?>