You will get an "Wrong parameter count" error (PHP 4 and possibly 5) if your array looks like the following:
min(115.23,432.11,0.00,45.76)
The 0.00 creates the error. Convert the 0.00 to a high number such as 10000000000.00 or remove it from the array before running the min() function.
min
(PHP 4, PHP 5)
min — 最小値を返す
説明
mixed min
( array $values
)
mixed min
( mixed $value1
, mixed $value2
[, mixed $value3...
] )
パラメータとして配列をひとつだけ渡した場合は、 min() は配列の中で最も大きい数値を返します。 ふたつ以上のパラメータを指定した場合は、min() はそれらの中で最も小さいものを返します。
注意: PHP は、数値として解釈できない string を integer と比較する場合には 0 と評価します。しかし、もしそれが最小値であった 場合、返り値はもとの文字列となります。0 と評価される 引数が複数存在した場合、mix() はもしその中に 文字列があればアルファベット順で一番小さな文字列の値を返し、 そうでなければ数値の 0 が返されます。
パラメータ
- values
-
値を含む配列。
返り値
min() は、パラメータとして渡した値の中で 数値として最も小さいものを返します。
例
Example#1 min() の例
<?php
echo min(2, 3, 1, 6, 7); // 1
echo min(array(2, 4, 5)); // 2
echo min(0, 'hello'); // 0
echo min('hello', 0); // hello
echo min('hello', -1); // -1
// 複数の配列を渡すと、mix はその要素を左から順に比較します。
// この例では 2 == 2 ですが 4 < 5 となります。
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)
// 配列と配列でない値が渡された場合は常に
// 配列が最大と判定されるため、配列が返されることはありません。
$val = min('string', array(2, 5, 7), 42); // string
?>
min
johngreenbury at australianescapes dot com dot au
30-Jan-2008 10:43
30-Jan-2008 10:43
piotr_sobolewski at o2 dot nospampleasenono dot pl
08-Nov-2007 01:11
08-Nov-2007 01:11
Be very careful when your array contains both strings and numbers. This code works strange (even though explainable) way:
var_dump(max('25.1.1', '222', '99'));
var_dump(max('2.1.1', '222', '99'));
dave at dtracorp dot com
14-Aug-2006 12:30
14-Aug-2006 12:30
empty strings '' will also return false or 0, so if you have something like
$test = array('', 1, 5, 8, 44, 22);
'' will be returned as the lowest value
if you only want to get the lowest number, you'll have to resort to the old fashioned loop
// default minimum value
$minVal = 100;
foreach ($test as $value) {
if (is_numeric($value) && $value < $minVal) {
$minVal = $value;
}
johnphayes at gmail dot com
03-May-2006 01:26
03-May-2006 01:26
Regarding boolean parameters in min() and max():
(a) If any of your parameters is boolean, max and min will cast the rest of them to boolean to do the comparison.
(b) true > false
(c) However, max and min will return the actual parameter value that wins the comparison (not the cast).
Here's some test cases to illustrate:
1. max(true,100)=true
2. max(true,0)=true
3. max(100,true)=100
4. max(false,100)=100
5. max(100,false)=100
6. min(true,100)=true
7. min(true,0)=0
8. min(100,true)=100
9. min(false,100)=false
10. min(100,false)=false
11. min(true,false)=false
12. max(true,false)=true
steffen at morkland dot com
16-Mar-2006 07:16
16-Mar-2006 07:16
> NEVER EVER use this function with boolean variables !!!
> Or you'll get something like this: min(true, 1, -2) == true;
> Just because of:
> min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) == true;
It is possible to use it with booleans, there is is just one thing, which you need to keep in mind, when evaluating using the non strict comparison (==) anyting that is not bool false, 0 or NULL is consideret true eg.:
(5 == true) = true;
(0 == true) = false;
true is also actually anything else then 0, false and null. However when true is converted to a string or interger true == 1, therefore when sorting true = 1. But if true is the maximum number bool true is returned. so to be sure, if you only want to match if true is the max number remember to use the strict comparison operater ===
01-Feb-2006 02:37
NEVER EVER use this function with boolean variables !!!
Or you'll get something like this: min(true, 1, -2) == true;
Just because of:
min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) == true;
You are warned !
DASPRiD d [AT] sprid [DOT] de
06-Jul-2005 10:39
06-Jul-2005 10:39
Here is my slightly modified version of the bugfree min-version. Now the max() function is no longer used in the modification and overall it's fasten up. Would be nice to get some feedback.
<?php
function min_mod () {
$args = func_get_args();
if (!count($args)) return false;
else {
$min = false;
foreach ($args AS $value) {
$curval = floatval($value);
if ($curval < $min || $min === false) $min = $curval;
}
}
return $min;
}
?>
alx5000 at walla dot com
13-Jan-2005 10:16
13-Jan-2005 10:16
If you want min to return zero (0) when comparing to a string, try this:
<?php
min(3,4,";"); // ";"
min(0,min(3,4,";")) // 0
?>
nonick AT 8027 DOT org
25-Jan-2004 12:43
25-Jan-2004 12:43
I tested this with max(), but I suppose it applies to min() too: If you are working with numbers, then you can use:
$a = ($b < $c) ? $b : $c;
which is somewhat faster (roughly 16%) than
$a = min($b, $c);
I tested this on several loops using integers and floats, over 1 million iterations.
I'm running PHP 4.3.1 as a module for Apache 1.3.27.
browne at bee why you dot ee dee you
17-Dec-2003 07:30
17-Dec-2003 07:30
min() can be used to cap values at a specific value. For instance, if you're grading papers and someone has some extra credit, but that shouldn't make it to the final score:
$pts_possible = 50;
$score = 55;
// Percent will equal 1 if $score/$pts_possible is greater than 1
$percent = min($score/$pts_possible,1);
kieran at mgpenguin dot net
19-Jul-2003 12:28
19-Jul-2003 12:28
Further modifications to the minnum function above.. This is for a project where I had to grab an entire column out of a database consisting of values that might be string, might be string representations of numbers (floating point or integer) or might be NULL, and find the minimum NUMERIC value:
function minnum($numarray){
//dont use min(), it contains a bug!
$min=0;
if ( ! is_array($numarray) ) $numarray = func_get_args();
if(is_array($numarray)==true){
$min=max($numarray);
for($z=0;$z<count($numarray);$z++){
$curval=floatval($numarray[$z]);
if(($curval != 0) && ($curval < $min)){
$min=$curval;
}
}
}
return $min;
}
Gets the floating point value of each entry and uses this to check whether it's actually a number before checking whether it's the minimum or not. Also contains modifications noted above to use it as a drop in replacement for min - ie multiple values passed.
Merome at wanadoo dot fr
06-Jul-2003 01:40
06-Jul-2003 01:40
Caution : it seems that min() can return a string :
min(";",50)=";" (I expected zero)
calin at php9 dot com
31-May-2003 12:19
31-May-2003 12:19
if you have an array like this
$arSrc[0]=14;
$arSrc[1]=16;
$arSrc[2]=13;
$arSrc[3]=17;
then in order to get the min element and its position in the array you can do:
$iMinValue = min($arSrc);
$arFlip = array_flip($arSrc);
$iMinPosition = $arFlip[$iMinValue];
echo
'<br />min_value=',
$iMinValue,
'<br />min_position=',
$iMinPosition
;
this example works for also for an associative array; of course with numeric values
09-Jul-2002 06:36
Re: above example - for a proper drop in replacement for the above, insert
if ( ! is_array($numarray) )
$numarray = func_get_args();
after
$min=0;
(For PHP3, check
if (intval(PHP_VERSION) >= 4 && ! is_array($numarray))
$numarray = func_get_args();
)
kevin at pricetrak dot com
09-Apr-2002 03:47
09-Apr-2002 03:47
The 'undefined' behaviour can bit you badly. I would expect min(undefined, -1000) to return -1000. Not so.
nak2 at mail2000 dot ru
15-Mar-2002 04:36
15-Mar-2002 04:36
If one of elements is undefided, min() result is underfinded too