how can I select the array with the oldest time
I have these arras
[0] => Array
(
[TEAM] => Array
(
[id] => 5
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09开发者_Go百科-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
)
)
I want to get this
[0] => Array
(
[TEAM] => Array
(
[id] => 5
[name] => localhost
)
[Registraion] => Array
(
[Registered] => 2011-09-20 09:20:51
)
)
as that person is the oldest to register.
How can I get the oldest registration value?
thanks
$oldestkey = null;
foreach (array_keys($array) as $key) {
if (isnull($oldestkey) || ($array[$key]['Registraion']['Registered'] < $array[$oldestkey]['Registraion']['Registered']) {
$oldestkey = $key;
}
}
Note that your key Registraion
is mis-spelled, I'm guessing it should be Registration
? Also note that this code will not handle the case where there's multiple keys with the same registration time. It'll pick out the FIRST oldest time and return the key for that record. Any duplicate times will be ignored.
Loop each item
$oldest = $arr[0];
foreach($array as $arr){
if($arr["Registration"]["Registered"] < $oldest["Registration"]["Registered"])
$oldest = $arr;
}
Please use the time comparision while comparing
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];
}
function above accepts your array, then loops through it and compare dates, and it will return last registered user.
精彩评论