简体   繁体   English

查找混合数组的最高max()

[英]Find highest max() of mixed array

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 我想要做的是为每个驱动程序输出其名称和最快的时间,例如

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

Jenson Button, time2, time3 - 41 简森·巴顿(Jenson Button),时间2,时间3-41

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, 我当时在使用max()但由于第一个元素是字符串而不是int / num而遇到麻烦,因此无法使其正常工作,

any idea how i would write this function ? 任何想法我将如何编写此功能?

Okay here you go, you were right to try with max() 好了,您可以尝试使用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 />';
}

EDIT: 编辑:

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()换为min()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM