疯狂码农/previous-current-iterator

一个简单的包,提供了一个用于比较前一个与当前项的迭代器

1.1.2 2019-07-11 16:42 UTC

This package is auto-updated.

Last update: 2024-09-12 03:44:55 UTC


README

Build StatusLatest Stable Version Total Downloads Latest Unstable Version License

前/当前迭代器

允许迭代数组,但每次返回两个元素,每次前进一个元素。因此,对于

['a', 'b', 'c', 'd']

将返回

['previous' => 'a', 'current' => 'b']
['previous' => 'b', 'current' => 'c']
['previous' => 'c', 'current' => 'd']

当前/下一个迭代器

允许迭代数组,但每次返回两个元素,每次前进一个元素。因此,对于

['a', 'b', 'c', 'd']

将返回

['current' => 'a', 'next' => 'b']
['current' => 'b', 'next' => 'c']
['current' => 'c', 'next' => 'd']
['current' => 'd', 'next' => null]

安装

要安装它,只需将此要求包含到您的 composer.json 中

{
    "require": {
        "crazycodr/previous-current-iterator": "1.*"
    }
}

然后根据需要运行 composer install/update。

支持

由于 key() 只能从 PHP 5.5 开始返回以数组开头的数组,因此只能支持 PHP 5.5 或更高版本!

示例

要使用 PreviousCurrentIterator,只需使用数组创建一个实例,并遍历它!

$data = ['a', 'b', 'c', 'd'];
foreach(new PreviousCurrentIterator($data) as $keys => $values) {
    //Compare previous and current
    if ($values['previous'] !== $values['current']) {
        echo 'Not the same<br />';
    }
}

要使用 CurrentNextIterator,只需使用数组创建一个实例,并遍历它!

$data = ['a', 'b', 'c', 'd'];
foreach(new CurrentNextIterator($data) as $keys => $values) {
    //Compare previous and current
    if ($values['next'] !== null) {
        if ($values['current'] !== $values['next']) {
            echo 'Not the same<br />';
        }
    }
}

用例

如果您需要以前一个与当前或当前与下一个的方式比较两个项,则实际。