查找混合数组的最高max() [英] Find highest max() of mixed array

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

问题描述

我已经从数据库中输出了一个数组

Ive got an array outputted from the DB

Array

    (
        [0] => Array
            (
                [name] => Fernando Alonso
                [time1] => 3.25
                [time2] => 3.25
                [time3] => 3.5
            )

        [1] => Array
            (
                [name] => Jenson Button
                [time1] => 34
                [time2] => 41
                [time3] => 41
            )
    )

我要做的是为每个驱动程序输出其名称和最快的时间

what i want to do is for each driver output their name and their fastest time somthing like

费尔南多·艾尔索索,时间3-3.5

Fernando Alsonso, time3 - 3.5

Jenson Button,时间2,时间3-41

Jenson Button, time2, time3 - 41

我一直在使用max()玩耍,但是由于第一个元素是字符串而不是int/num,因此遇到了麻烦,因为它遇到了麻烦

I was playing around using max() but was having trouble getting it to work as it was having trouble as the first element is a string, rather than a int / num,

知道我将如何编写此函数吗?

any idea how i would write this function ?

推荐答案

好的,您可以尝试使用max()

Okay here you go, you were right to try with max()

function get_highest_time($driver) {
    // First find the highest time
    $highest_time = max($driver['time1'], $driver['time2'], $driver['time3']);
    // now find all of the array keys that match that time
    $times = array_keys($driver, $highest_time);
    // now turn that array of keys into a string and add the highest time to the end
    return implode(', ', $times) . ' - ' . $highest_time;
}

foreach ($arr as $driver) { // Where $arr is your array of drivers
    print $driver['name'] . ': ' . get_highest_time($driver) . '<br />';
}

只是注意到您说过您想要驾驶员最快的时间,那肯定是最低的时间吗?在这种情况下,将max()替换为min()

Just noticed you said you want the driver's fastest time, which would surely be the lowest number? If that is the case swap max() for min()

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

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