2011-09-20 44 views
0

數組我有這些掛毯我怎麼能選擇具有最古老時間

[0] => Array 
     (
      [TEAM] => Array 
       (
        [id] => 5 
        [name] => localhost 

       ) 

      [Registraion] => Array 
       (

        [Registered] => 2011-09-20 09:20:51 
       ) 

     ) 

[1] => Array 
     (
      [TEAM] => Array 
       (
        [id] => 6 
        [name] => localhost 

       ) 

      [Registraion] => Array 
       (

        [Registered] => 2011-09-20 09:30:51 
       ) 


     ) 

[2] => Array 
     (
      [TEAM] => Array 
       (
        [id] => 7 
        [name] => localhost 

       ) 

      [Registraion] => Array 
       (

        [Registered] => 2011-09-20 09:40:51 
       ) 


     ) 

我想這

[0] => Array 
      (
       [TEAM] => Array 
        (
         [id] => 5 
         [name] => localhost 

        ) 

       [Registraion] => Array 
        (

         [Registered] => 2011-09-20 09:20:51 
        ) 

      ) 

因爲那個人是最古老的註冊。

如何獲取最舊的註冊值?

感謝

+1

在數組上循環並記住目前爲止您已經看到的最早時間的元素的id和時間。在循環結束時,你知道答案。 – Jon

回答

2
$oldestkey = null; 

foreach (array_keys($array) as $key) { 
    if (isnull($oldestkey) || ($array[$key]['Registraion']['Registered'] < $array[$oldestkey]['Registraion']['Registered']) { 
     $oldestkey = $key; 
    } 
} 

請注意,你的關鍵Registraion是拼寫錯誤,我猜應該是Registration?另請注意,此代碼不會處理具有相同註冊時間的多個密鑰的情況。它會選出第一個最早的時間,並返回該記錄的密鑰。任何重複的時間將被忽略。

1

循環的每個項目

$oldest = $arr[0]; 
foreach($array as $arr){ 
     if($arr["Registration"]["Registered"] < $oldest["Registration"]["Registered"]) 
      $oldest = $arr; 
} 

請使用時間對比,同時比較

1
function getOldestRecord($ar) 
{ 
    $last_id; 
    $last_time = 0; 
    foreach($ar as $key => $val) 
    { 
     $time_stamp = strtotime($val['Registration']['Registered']); 
     if($time_stamp > $last_time) 
     { 
     $last_time = $time_stamp; 
     $last_id = $key; 
     } 
    } 
    return $ar[$last_id]; 
} 

上述函數接受您的數組,然後通過它循環和比較日期,這將最後註冊的用戶返回。