日期:2014-05-17 浏览次数:20691 次
$arr1里面是从数据库里取出来的数据,print_r出来如下:
Array
(
[0] => 30544
[1] => 30544
[2] => 30532
[3] => 30550
[4] => 30544
[5] => 30544
[6] => 30532
[7] => 30532
[8] => 30532
[9] => 30550
[10] => 30544
[11] => 30544
[12] => 30544
[13] => 30544
[14] => 30544
[15] => 30544
[16] => 30532
[17] => 30532
[18] => 30532
[19] => 30532
)
$arr2 = array('1','2','4','5');
//我用同一个函数 max()取最大值,结果如下:
//arr1的结果,会报错,但是可以取到数据
30550
A PHP Error was encountered
Severity: Warning
Message: max() [function.max]: When only one parameter is given, it must be an array
Filename: test.php
Line Number: 104
//arr2正常,输出
5
logminMath 函数
PHP Manual
--------------------------------------------
max
(PHP 4, PHP 5)
max — 找出最大值
说明
mixed max ( number $arg1 , number $arg2 )
mixed max ( array $numbers [, array $... ] )
max() 返回参数中数值最大的值。
如果仅有一个参数且为数组,max() 返回该数组中最大的值。如果第一个参数是整数、字符串或浮点数,则至少需要两个参数而 max() 会返回这些值中最大的一个。可以比较无限多个值。
Note:
PHP 会将非数值的 string 当成 0,但如果这个正是最大的数值则仍然会返回一个字符串。如果多个参数都求值为 0 且是最大值,max() 会返回其中数值的 0,如果参数中没有数值的 0,则返回按字母表顺序最大的字符串。
Example #1 使用 max() 的例子
<?php
echo max(1, 3, 5, 6, 7); // 7
echo max(array(2, 4, 5)); // 5
echo max(0, 'hello'); // 0
echo max('hello', 0); // hello
echo max(-1, 'hello'); // hello
// 对多个数组,max 从左向右比较。
// 因此在本例中:2 == 2,但 4 < 5
$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7)
// 如果同时给出数组和非数组作为参数,则总是将数组视为
// 最大值返回
$val = max('string', array(2, 5, 7), 42); // array(2, 5, 7)
?>
参见 min() 和 count()。
User Contributed Notes
sun at drupal dot org 03-Aug-2011 11:25
Note that max() throws a warning if the array is empty:
<?php
$a = array();
max($a);
// Warning: max(): Array must contain at least one element
?>
So make sure your data isn't empty.
php at rijkvanwel dot nl 12-Apr-2011 02:08
To get the largest key in an array:
<?php
$array = array( 0 => 'first', 1=> 'second', /* ... */ 99 => 'nth' );
$max_key = max( array_keys( $array ) ); // 99
?>
Alex Stanhope 28-Oct-2010 03:00
If you want to test whether x lies within two bounds:
<?php
static function isInRange($x, $y1, $y2) {
return( ($x >= min($y1, $y2)) && ($x <= max($y1, $y2)) );
}
//called by:
class::isInRange(10,12,2);
//or
$this->isInRange(10,12,2);
//or (drop static)
isInRange(10,12,2);
//simple test function:
static function unit_isInRange() {
$output = array();
$inputlist[] = array(10, 12, 2, true);
$inputlist[] = array(13, 12, 2, false);
$inputlist[] = array(2, -2, 2, true);
$inputlist[] = array(2, -8, -2, false);
foreach($inputlist as $input) {
$output[] = array(
'input' => array($input[0], $input[1], $input[2]),
'output' => self::isInRange($input[0],$input[1],$input[2]),
'expected' => $input[3],
);
}
return($output);
}
?>
dan at coders dot co dot nz 22-May-2010 11:29
max() on undefined parameters does not assume the value is zero, it ignores it.
<?php
$dimensions = array('left' => -2);
// If $dimensions['right'] was never set,
// we may expect it to be treated as zero, but
print max($dimensions['left'], $dimensions['right']);
//
// Returns -2, not zero
print max(0+$dimensions['left'], 0+$dimensions['right']);
?>
would be a work-around, but it's probably tidier to ensure your values are set c