min

(PHP 4, PHP 5, PHP 7, PHP 8)

minFind lowest value

Açıklama

min(mixed $value, mixed ...$values): mixed

Alternative signature (not supported with named arguments):

min(array $value_array): mixed

If the first and only parameter is an array, min() returns the lowest value in that array. If at least two parameters are provided, min() returns the smallest of these values.

Bilginize:

Values of different types will be compared using the standard comparison rules. For instance, a non-numeric string will be compared to an int as though it were 0, but multiple non-numeric string values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied.

Dikkat

Be careful when passing arguments of different types because min() can produce unpredictable results.

Bağımsız Değişkenler

value

Any comparable value.

values

Any comparable values.

value_array

An array containing the values.

Dönen Değerler

min() returns the parameter value considered "lowest" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. 0 and 'abc') the first provided to the function will be returned.

Hatalar/İstisnalar

If an empty array is passed, min() throws a ValueError.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 min() throws a ValueError on failure now; previously, false was returned and an E_WARNING error was emitted.
8.0.0 As string to number comparisons were changed, min() no longer returns a different value based on the argument order in those cases.

Örnekler

Örnek 1 Example uses of min()

<?php
echo min(2, 3, 1, 6, 7); // 1
echo min(array(2, 4, 5)); // 2

// Here we are comparing -1 < 0, so -1 is the lowest value
echo min('hello', -1); // -1

// With multiple arrays of different lengths, min returns the shortest
$val = min(array(2, 2, 2), array(1, 1, 1, 1)); // array(2, 2, 2)

// Multiple arrays of the same length are compared from left to right
// so in our example: 2 == 2, but 4 < 5
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)

// If both an array and non-array are given, the array is never returned
// as comparisons treat arrays as greater than any other value
$val = min('string', array(2, 5, 7), 42); // string

// If one argument is NULL or a boolean, it will be compared against
// other values using the rules FALSE < TRUE and NULL == FALSE regardless of the
// other types involved
// In the below examples, both -10 and 10 are treated as TRUE in the comparison
$val = min(-10, FALSE, 10); // FALSE
$val = min(-10, NULL, 10); // NULL

// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE
$val = min(0, TRUE); // 0
?>

Ayrıca Bakınız

  • max() - Find highest value
  • count() - Bir dizideki veya bir Countable nesnesindeki eleman sayısını döndürür

add a note

User Contributed Notes 12 notes

up
5
volch5 at gmail dot com
10 years ago
min() (and max()) on DateTime objects compares them like dates (with timezone info) and returns DateTime object.
<?php
$dt1
= new DateTime('2014-05-07 18:53', new DateTimeZone('Europe/Kiev'));
$dt2 = new DateTime('2014-05-07 16:53', new DateTimeZone('UTC'));
echo
max($dt1,$dt2)->format(DateTime::RFC3339) . PHP_EOL; // 2014-05-07T16:53:00+00:00
echo min($dt1,$dt2)->format(DateTime::RFC3339) . PHP_EOL; // 2014-05-07T18:53:00+03:00
?>

It works at least 5.3.3-7+squeeze17
up
3
Anonymous
18 years ago
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 !
up
-1
harmor
16 years ago
A way to bound a integer between two values is:

function bound($x, $min, $max)
{
return min(max($x, $min), $max);
}

which is the same as:

$tmp = $x;
if($tmp < $min)
{
$tmp = $min;
}
if($tmp > $max)
{
$tmp = $max;
}
$y = $tmp;

So if you wanted to bound an integer between 1 and 12 for example:

Input:
$x = 0;
echo bound(0, 1, 12).'<br />';
$x = 1;
echo bound($x, 1, 12).'<br />';
$x = 6;
echo bound($x, 1, 12).'<br />';
$x = 12;
echo bound($x, 1, 12).'<br />';
$x = 13;
echo bound($x, 1, 12).'<br />';

Output:
1
1
6
12
12
up
-2
steffen at morkland dot com
18 years ago
> 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 ===
up
-3
DO
16 years ago
I've modified the bugfree min-version to ignore NULL values (else it returns 0).

<?php
function min_mod () {
$args = func_get_args();

if (!
count($args[0])) return false;
else {
$min = false;
foreach (
$args[0] AS $value) {
if (
is_numeric($value)) {
$curval = floatval($value);
if (
$curval < $min || $min === false) $min = $curval;
}
}
}

return
$min;
}
?>
up
-4
Teelevision
10 years ago
A function that returns the lowest integer that is not 0.
<?php
/* like min(), but casts to int and ignores 0 */
function min_not_null(Array $values) {
return
min(array_diff(array_map('intval', $values), array(0)));
}
?>
up
-3
piotr_sobolewski at o2 dot nospampleasenono dot pl
16 years ago
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'));
up
-4
hava82 at gmail dot com
13 years ago
Here is function can find min by array key

<?php
function min_by_key($arr, $key){
$min = array();
foreach (
$arr as $val) {
if (!isset(
$val[$key]) and is_array($val)) {
$min2 = min_by_key($val, $key);
$min[$min2] = 1;
} elseif (!isset(
$val[$key]) and !is_array($val)) {
return
false;
} elseif (isset(
$val[$key])) {
$min[$val[$key]] = 1;
}
}
return
min( array_keys($min) );
}
?>
up
-5
php at keith tyler dot com
13 years ago
If NAN is the first argument to min(), the second argument will always be returned.

If NAN is the second argument, NAN will always be returned.

The relationship is the same but inverted for max().

<?php
// \n's skipped for brevity
print max(0,NAN);
print
max(NAN,0);
print
min(0,NAN);
print
min(NAN,0);
?>

Returns:
0
NAN
NAN
0
up
-5
8ilO
7 years ago
A min_by function:
<?php
function min_by(Array $arr, Callable $func){
$mapped = array_map($func, $arr);
return
$arr[array_search(min($mapped), $mapped)];
}
$a = ["albatross", "dog", "horse"];
echo
min_by($a, "strlen"); // dog
?>
up
-5
matt at borjawebs dot com
13 years ago
A condensed version (and possible application) of returning an array of array keys containing the same minimum value:

<?php
// data
$min_keys = array();
$player_score_totals = array(
'player1' => 300,
'player2' => 301,
'player3' => 302,
'player4' => 301,
...
);

// search for array keys with min() value
foreach($player_score_totals as $playerid => $score)
if(
$score == min($player_score_totals)) array_push($min_keys, $playerid);

print_r($min_keys);
?>
up
-5
nonick AT 8027 DOT org
20 years ago
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.
To Top