php数组奇怪的问题
如下:
php code$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的结果,会报错,但是可以取到数据30550a php error was encounteredseverity: warningmessage: max() [function.max]: when only one parameter is given, it must be an arrayfilename: test.phpline number: 104//arr2正常,输出5
以上,报错是为什么啊,怎么解决啊。谢谢。
------解决方案--------------------
max() [function.max]: when only one parameter is given, it must be an array
max一个参数的时候,必须是个数组。
php codelogminmath 函数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() 的例子 '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: = min($y1, $y2)) && ($x 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. -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 correctly first. (on php 5.2.11 anyway) alex rath 10-apr-2010 11:27 notice that whenever there is a number in front of the string, it will be used for comparison.but just if it is in front of the string grillen at abendstille dot at 02-apr-2010 03:55 max only accepts not empty arrays. if you work a lot with numerical arrays and with max, this function may come in handy: artem dot yagutyan at gmail dot com 27-mar-2010 03:28 this is good example: for max to min $val) { if ($val == max($array)) return $key; }}$array = array(10, 2, 5, 7, 4,15,32,8,41,25);$array_count=count($array); for($i=1;$i; // }/* output41 // max3225151087542 //min*/?> marcini 11-may-2009 07:34 note that max() can compare dates, so if you write something like this:you will get: 2009-03-15. ries at vantwisk dot nl 09-nov-2008 06:36 i had several occasions that using max is a lot slower then using a if/then/else construct. be sure to check this in your routines!ries marcus zacco 29-sep-2008 09:47 this code loops through seven arrays and finds the highest average value within those arrays - and changes the font color for it. great for highlighting. the biggest take-away is this the row if($average[$i] == max($average)) the number_format just rounds the numbers to 0 decimal points. $maxindex); } ?> jeremi23 at gmail dot com 14-jun-2007 03:09 max on a an array with key/values 5, 2=> 3); echo max($tmp); ?> this return 5, so the max is done on the values. johnmott59 at hotmail dot com 17-may-2007 12:35 to find the maximum value from a set of 1-dimensional arrays, do this: the inner max() functions operate on the arrays, the outer max compares the numeric results of the inner ones. johnphayes at gmail dot com 02-may-2006 09:27 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)=true2. max(true,0)=true3. max(100,true)=1004. max(false,100)=1005. max(100,false)=1006. min(true,100)=true7. min(true,0)=08. min(100,true)=1009. min(false,100)=false10. min(100,false)=false11. min(true,false)=false12. max(true,false)=true --------------------------------------------logminmath 函数php manual