我有一个从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
)
)
我想要做的是为每个司机输出他们的名字和最快的时间,比如
费尔南多·阿尔索索,时间3 - 3.5简森巴顿,时间2,时间3 - 41
我正在使用max()
进行游戏但由于第一个元素是字符串而不是int / num,因此遇到麻烦而无法正常工作
任何想法我怎么写这个功能?
答案 0 :(得分: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()