php自己动手实现max,找出数组中最大的值
php中对数组进行最大值输出,可以使用max()方法。然而,有时候,自己动手实现一次系统函数,也是一件愉快的事情,接下来我使用php代码来实现一个自定义函数:getMaxValue(array $arr)。该函数需要提供一个数组,程序就会返回数组中数值最大的那个数值。源码如下:
<?php /** * Created by PhpStorm. * User: fedkey * Date: 18-10-26 * Time: 下午12:17 */ class ClassDemo { public function getMaxValue(array $arr) { $maxValue = null; $indexLength = count($arr) - 1; //遍历数组$arr for ($a = 0; $a < $indexLength; $a++) { //索引号为$a的和索引号为 $a+1进行大小比较 if ($arr[$a] >= $arr[$a + 1]) { //只有当$maxValue比$arr[$a]小时才改变$maxValue的值 if ($maxValue < $arr[$a]) { $maxValue = $arr[$a]; } } else { if ($maxValue < $arr[$a + 1]) { $maxValue = $arr[$a + 1]; } } } return $maxValue; } } $demo = new ClassDemo(); echo $demo->getMaxValue([-9122, -10, -4, -9, -54, -6, -150, -2054, -32230, -33, -222, -55, -22, -10]);
输出结果:-4
更多阅读
- 宇秀下拉 2021-5-3下拉更新案例, 不再定期发布案例
- 2019新ad key及下拉词格式对应教程
- electron 报错 Error: Electron failed to install correctly, please delete node_modulees/electron and try installing again
- 百度搜索重大事件及seo算法大全(2015~2018)【值得收藏】
- CI 在nginx中出现404错误的解决方式
- 如何选择关键词做下拉?
- 背锅,2018年淘宝店被差评
- 使用百度竞价信息采集与排名查询助手,结果少或没有结果的可能的原因总结
- centos apache安装wordpress插件出现需要输入ftp帐号的问题(已解决)
- archLinux安装fcitx输入法不能切换中文问题(已解决)

qq:1535604235