max() - 找出最大值 - php 数学函数
max()
(PHP 4, PHP 5, PHP 7)
找出最大值
说明
max(array $values): mixedmax(mixed $value1,mixed $value2[,mixed$...
]): mixed如果仅有一个参数且为数组,max()返回该数组中最大的值。如果第一个参数是整数、字符串或浮点数,则至少需要两个参数而max()会返回这些值中最大的一个。可以比较无限多个值。
Note:PHP 会将非数值的string当成0,但如果这个正是最大的数值则仍然会返回一个字符串。如果多个参数都求值为0且是最大值,max()会返回其中数值的0,如果参数中没有数值的0,则返回按字母表顺序最大的字符串。
参数
$values包含了多个值的数组。
$value1Anycomparablevalue.
$value2Anycomparablevalue.
...Anycomparablevalue.
返回值
max()返回参数中数值最大的值。 If multiple values can be considered of the same size, the one that is listed first will be returned.
Whenmax()is given multiplearrays, the longest array is returned. If all the arrays have the same length,max()will use lexicographic ordering to find the return value.
When given astringit will be cast as anintegerwhen comparing.
范例
使用max()的例子
参见
min()
找出最小值count()
计算数组中的单元数目,或对象中的属性个数
The simplest way to get around the fact that max() won't give the key is array_search: This could also be done with array_flip, though overwriting will mean that it gets the last max value rather than the first: To get all the max value keys:
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
max(null, 0) = null max(0, null) = 0
max() (and min()) on DateTime objects compares them like dates (with timezone info) and returns DateTime object. It works at least 5.3.3-7+squeeze17
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
Be aware if a array like this is used (e.g. values from a shopping cart): The result will be: 39,27 and not - as expected - 175,80 So, to find the max value, use integer only like: and you will get the correct result: 17580
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
Sometimes you could need to get the max from an array which looks like this: all max functions i see around don't provide a way to get the max value of the values in (example) key "b" output: 43
To get the largest key in an array:
Matlab users and others may feel lonely without the double argument output from min and max functions. To have the INDEX of the highest value in an array, as well as the value itself, use the following, or a derivative:
In response to: keith at bifugi dot com If you want to find the specific key(s) that match the maximum value in an array where the values may be duplicated, you can loop through and perform a simple check: This produces consistent results and will scale well in terms of performance, whereas functions like array_search and array_flip can lead to degraded performance when dealing with large amounts of data.
Note that max() throws a warning if the array is empty: So make sure your data isn't empty.
A way to bound a integer between two values is: which is the same as: So if you wanted to bound an integer between 1 and 12 for example: Input: Output: 1 1 6 12 12
Note that max() can compare dates, so if you write something like this: you will get: 2009-03-15.
A little function for multi-dimensional arrays:
- This function is not actually recursive, but fulfills the requirement that it works on sub-arrays. I do this because PHP is not very good at recursion. I also did it because I enjoy doing things this way. - It returns something of type int, never a string representation of an int. The exception is when you provide an array that does not contain any integers. It will then return NULL. - It ignores non-array, non-int values.
Be careful using max() with objects, as it returns a reference not a new object.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!