PHP 从数组中获取最大值 [英] PHP Get Highest Value from Array

查看:49
本文介绍了PHP 从数组中获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取数组中的最大值,同时仍保留项目标签.我知道我可以通过运行 sort() 来做到这一点,但如果我这样做,我只会丢失标签——这使得我需要的东西毫无意义.这是数组:

I'm trying to get hold of the largest value in an array, while still preserving the item labels. I know I can do this by running sort(), but if I do so I simply lose the labels - which makes it pointless for what I need. Here's the array:

array("a"=>1,"b"=>2,"c"=>4,"d"=>5);

有什么想法吗?

推荐答案

不要对数组进行排序以获得最大值.

Don't sort the array to get the largest value.

获取最大值:

$value = max($array);

获取对应的key:

$key = array_search($value, $array);

这篇关于PHP 从数组中获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆