我有一个数组,创建时间已经按 AES 排序。
$list = array(0 => array('createtime' => 90,'message'=>'ad','user'=>'a'),
1 => array('createtime' => 91,'message'=>'ae','user'=>'b'),
2 => array('createtime' => 93,'message'=>'ae','user'=>'b'),
3 => array('createtime' => 93,'message'=>'ae','user'=>'c'),
4 => array('createtime' => 94,'message'=>'ah','user'=>'d'),
5 => array('createtime' => 99,'message'=>'ah','user'=>'a'),
6 => array('createtime' => 100,'message'=>'ah','user'=>'a'));
As you can see,maybe the array is from a sql table,what I want is to deal with these data,the result of array like this.
$list = array(0 => array('createtime' => 90,'message'=>'ad','user'=>'a'),
1 => array('createtime' => 91,'message'=>'ae+ae','user'=>'b'),
2 => array('createtime' => 93,'message'=>'ae','user'=>'c'),
3 => array('createtime' => 94,'message'=>'ah','user'=>'d'),
4 => array('createtime' => 99,'message'=>'ah+ah','user'=>'a'));
是的,你可能会发现 5 之间的创建时间和相同的用户数据组合在一起。我想要的就是这样,将来它可能包括更多的用户和更多的创建时间。我这样编码来处理它,但是当我更改我的数据(如添加用户或更改用户等)时效果不佳。
$diff = 0;
$count = 0;
$d = array();
$j = array();
for($i = 1;isset($list[$i]);){
if ($list[$i]['user'] != $list[$i-1-$count]['user']) {
if (!in_array($list[$i]['user'], $d)) {
$d[$diff] = $list[$i]['user'];
$j[$diff] = $i;
$count++;
$diff++;
$i++;
continue;
} else {
$i++;
$count++;
continue;
}
}
if($list[$i]['createtime'] - $list[$i-1-$count]['createtime'] < 5){
$list[$i-1-$count]['message'] = $list[$i-1-$count]['message'].'<br>'.$list[$i]['message'];
array_splice($list,$i,1);
} else if ($count == 0) {
$i++;
} else {
$i = $j[0] + 1;
$count = 0;
$diff = 0;
$d = array();
$j = array();
}
}
好像有问题。那我应该怎么做才能满足我的要求。还要考虑短创建时间之间的更多用户。谢谢。