0

我有一个从数据库输出的数组

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

我想做的是为每个司机输出他们的名字和他们最快的时间之类的

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

简森·巴顿,时间 2,时间 3 - 41

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

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

4

1 回答 1

1

好的,你去,你是对的尝试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()

于 2013-05-08T16:11:02.883 回答