php根据某一键值去除二维数组中重复值
可能听起来有点绕,具体的数组是这样的:array(10){[0]=>array(2){["role_id"]=>string(5)"48357"["login_time"...
可能听起来有点绕,具体的数组是这样的:
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又登陆一次,这样就会有在数组里面有两个记录,但是这只能算一次,请教高手这数组怎么处理才能满足要求啊? 展开
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又登陆一次,这样就会有在数组里面有两个记录,但是这只能算一次,请教高手这数组怎么处理才能满足要求啊? 展开
4个回答
展开全部
简单处理一下,只保留较新的登录时间。而且无重复。
<?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)
<?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)
展开全部
2楼的意思是把数据放到数据库里,然后group by role_id,根据你的需求然后count$query.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
性能肯定高不了.就算是php内置的函数,处理方法也都差不多.
如果是数据库呢.可能稍微好一点.
如果是数据库呢.可能稍微好一点.
追问
您的如果是数据库,可能稍微好一点怎么理解呢 呵呵
追答
如果数据时存在数据库中。那就很好操作了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不知道这个数组里元素多不多。如果不考虑性能的话,需要循环。
另外建个数组,存唯一的role_id。
另外建个数组,存唯一的role_id。
追问
元素很多的!这种方法我也有考虑,还有没其他的什么好的方法?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询