Previ()和next(),同时循环PHP中的对象集合


Prev() and next() while looping through a collection of objects in PHP

所以我们都知道,当使用foreach迭代PHP数组时,可以使用prev()访问上一个索引,使用next()访问下一个索引。在我的例子中,我遍历一组对象,并在编程块中呈现相应的属性。

现在我需要将当前对象的属性与上一个对象的属性进行比较。从本质上讲,如果prev()是一个数组就足够了,但我正在尝试用PHP做同样的事情。

温馨提示!

谢谢,Parijat

$old = null;
foreach ( $objects as $object ) {
    if ( $old && $old->property == $object->property ) {
        foo();
    } else {
        bar();
    }
    $old = $object;
}