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


Find highest max() of mixed array

从数据库中得到一个数组输出

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
            )
    )

我想做的是为每个驱动程序输出他们的名字和他们最快的时间,比如

Fernando Alsonso, time3 - 3.5

简森按钮,time2, time3 - 41

我正在使用max(),但在让它工作时遇到了麻烦,因为它遇到了麻烦,因为第一个元素是字符串,而不是int/num,

知道我该怎么写这个函数吗?

好的,你试着用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()