可能听起来有点绕,具体的数组是这样的:
array(10) { [0]=> array(2) { ["role_id"]=> string(5) "48357" ["login_time"]=> string(19) "2011-08-14 00:02:26" } [1]=> array(2) { ["role_id"]=> string(5) "48357" ["login_time"]=> string(19) "2011-08-14 00:01:51" } [2]=> array(2) { ["role_id"]=> string(5) "48356" ["login_time"]=> string(19) "2011-08-14 00:02:25" } [3]=> array(2) { ["role_id"]=> string(5) "48358" ["login_time"]=> string(19) "2011-08-14 00:02:45" } [4]=> array(2) { ["role_id"]=> string(5) "48365" ["login_time"]=> string(19) "2011-08-14 00:05:52" } [5]=> array(2) { ["role_id"]=> string(5) "48369" ["login_time"]=> string(19) "2011-08-14 00:06:40" } [6]=> array(2) { ["role_id"]=> string(5) "48372" ["login_time"]=> string(19) "2011-08-14 00:08:36" } [7]=> array(2) { ["role_id"]=> string(5) "48368" ["login_time"]=> string(19) "2011-08-14 00:06:43" } [8]=> array(2) { ["role_id"]=> string(5) "48380" ["login_time"]=> string(19) "2011-08-14 00:10:15" } [9]=> array(2) { ["role_id"]=> string(5) "48377" ["login_time"]=> string(19) "2011-08-14 00:09:48" } [10]=> array(2) { ["role_id"]=> string(5) "48366" ["login_time"]=> string(19) "2011-08-14 00:07:08" }
记录的是role_id 登陆的记录,因为有可能一个role_id在2011-08-14 00:10:15登陆了,在2011-08-14 00:15:19又登陆一次,这样就会有在数组里面有两个记录,但是这只能算一次,请教高手这数组怎么处理才能满足要求啊?
<?php
$a=array(
array(
"role_id"=>"48357" ,
"login_time"=>"2011-08-14 00:02:26"
),
array(
"role_id"=>"48357" ,
"login_time" => "2011-08-14 00:01:51"
),
array(
"role_id"=> "48356",
"login_time"=> "2011-08-14 00:02:25"
),
array(
"role_id"=> "48358",
"login_time"=> "2011-08-14 00:02:45"
),
array(
"role_id"=> "48365",
"login_time"=> "2011-08-14 00:05:52"
),
array(
"role_id"=> "48369" ,
"login_time"=> "2011-08-14 00:06:40"
),
array(
"role_id"=> "48372",
"login_time"=> "2011-08-14 00:08:36"
),
array(
"role_id"=> "48368",
"login_time"=> "2011-08-14 00:06:43"
),
array(
"role_id"=> "48380" ,
"login_time"=> "2011-08-14 00:10:15"
),
array(
"role_id"=> "48377" ,
"login_time"=> "2011-08-14 00:09:48"
),
array(
"role_id"=> "48366" ,
"login_time"=> "2011-08-14 00:07:08"
)
);
$b=array();
foreach($a as $e){
$id=intval($e['role_id']);
$b[$id]=isset($b[$id])?
(strtotime($e['login_time'])>strtotime($b[$id]['login_time']))? $e:$b[$id] : $e;
}
$b=array_values($b);
var_dump($b);
=============
显示
array
0 =>
array
'role_id' => string '48357' (length=5)
'login_time' => string '2011-08-14 00:02:26' (length=19)
1 =>
array
'role_id' => string '48356' (length=5)
'login_time' => string '2011-08-14 00:02:25' (length=19)
2 =>
array
'role_id' => string '48358' (length=5)
'login_time' => string '2011-08-14 00:02:45' (length=19)
3 =>
array
'role_id' => string '48365' (length=5)
'login_time' => string '2011-08-14 00:05:52' (length=19)
4 =>
array
'role_id' => string '48369' (length=5)
'login_time' => string '2011-08-14 00:06:40' (length=19)
5 =>
array
'role_id' => string '48372' (length=5)
'login_time' => string '2011-08-14 00:08:36' (length=19)
6 =>
array
'role_id' => string '48368' (length=5)
'login_time' => string '2011-08-14 00:06:43' (length=19)
7 =>
array
'role_id' => string '48380' (length=5)
'login_time' => string '2011-08-14 00:10:15' (length=19)
8 =>
array
'role_id' => string '48377' (length=5)
'login_time' => string '2011-08-14 00:09:48' (length=19)
9 =>
array
'role_id' => string '48366' (length=5)
'login_time' => string '2011-08-14 00:07:08' (length=19)
如果是数据库呢.可能稍微好一点.追问
您的如果是数据库,可能稍微好一点怎么理解呢 呵呵
追答如果数据时存在数据库中。那就很好操作了。
另外建个数组,存唯一的role_id。追问
元素很多的!这种方法我也有考虑,还有没其他的什么好的方法?