这是我的循环
$i=0;
foreach($last5 as $last){
if($uid['urels_id'] != $last->urels_id ){
$i=0;
}
$uid = array('urels_id' => $last->urels_id, 'count' => $i);
if( $uid['count'] <= 4 && $uid['urels_id'] == $last->urels_id ){
$last_five['last5'][] = array('urels_id' => $last->urels_id, 'url' => $last->url, 'title' => $last->title, 'sent' => $last->sent);
}
else {
continue;
}
$i++;
array_push($gcntd, $last_five);
}
我正在尝试制作的是将每个id限制为5.每个urels_id可能有100个结果,我只想要最后5个.SQL查询限制不是一个选项。
它确实正确返回但返回所有结果
这是一个示例
[last5] => Array
(
[0] => Array
(
[urels_id] => 17366
[url] => /evl/reb/4575464991.html
[title] => Beautiful Remodel in Scottsdale (Scottsdale) $645000 4bd 2843sqft
[sent] => 2014-08-01 21:57:07
)
提前致谢
答案 0 :(得分:1)
跟踪ID遇到的次数?
$ids = array();
foreach($arr as $item) {
$ids[$item['id']]++; // increment id counter
if ($ids[$item['id']] < 5) {
... do something ...
}
}