关联数组的最高值 [英] Hightest value of an associative array

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

问题描述

有没有什么简单的方法可以获得关联数组的最高数值?

Is there any easy way to get the hightest numeric value of an associative array?

$array = array(
    0 => array(
        'key1' => '123',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    ),
    1 => array(
        'key1' => '124',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    ),
    2 => array(
        'key1' => '125',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    )
);

AwesomeFunction($array, 'key1'); // returns 2 ($array key)

请善待,因为这个问题是用手机写的.谢谢.

Please be kind since this question was written with a phone. Thanks.

推荐答案

如果您知道您的数据将始终采用这种格式,那么应该可以使用类似的方法.

If you know your data will always be in that format, something like this should work.

function getMax( $array )
{
    $max = 0;
    foreach( $array as $k => $v )
    {
        $max = max( array( $max, $v['key1'] ) );
    }
    return $max;
}

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

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