2013-05-08 61 views
0

Ive得到了來自DB輸出數組查找混合陣列的最高MAX()

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

我想做的事是每個驅動器輸出他們的名字和他們的時間最快的財產以後像

費爾南多Alsonso ,時間3 - 3.5

巴頓,時間2,時間3 - 41

我打得周圍使用max()但無法得到它的工作,因爲它是就吃克麻煩作爲第一個元素是一個字符串,而不是一個int/num,

任何想法如何我會寫這個功能?

回答

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

+0

工作過的魅力,謝謝! – sam 2013-05-08 16:16:36

+0

您是否看到我的編輯? – RMcLeod 2013-05-08 16:17:15

+0

是的,謝謝,對不起草率的措辭在我身邊 – sam 2013-05-08 16:19:48