如何获取foreach中下一个ORM元素的值? 我需要这样的东西:
$users = ORM::factory('Users')
->order_by('category', 'ASC')
->find_all();
foreach($users as $user)
{
if($user->category != next($user->category))
{
echo 'Next category user';
}
}
答案 0 :(得分:1)
有时候比较下一个项目很困难(比如这些)。但是,与前一个相比通常很容易。
E.g。像这样:
$users = ORM::factory('Users')
->order_by('category', 'ASC')
->find_all();
$previousCategory = null;
foreach($users as $user)
{
if($user->category != $previousCategory)
{
echo 'Next category user';
}
$previousCategory = $user->category;
}
答案 1 :(得分:0)
一种方法是手动搜索下一个元素,获取所需的值,然后重置回原来的位置。它很乱,但它会做你想要的:
foreach($users as $user)
{
$current_key = $users->key();
$next_category = $users->seek($current_key + 1) ? $users->current()->category : NULL;
$users->seek($current_key);
if($next_category && $user->category != $next_category)
{
echo 'Next category user';
}
}