2017 © Pedro Peláez
 

library previous-current-iterator

Simple package that offers an iterator used for previous vs current comparison

image

crazycodr/previous-current-iterator

Simple package that offers an iterator used for previous vs current comparison

  • Tuesday, October 6, 2015
  • by crazycodr
  • Repository
  • 1 Watchers
  • 0 Stars
  • 6,101 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 13 % Grown

The README.md

Build StatusLatest Stable Version Total Downloads Latest Unstable Version License, (*1)

Previous/Current iterator

Allows iteration over an array but returns two items at a time stepping by one item at a time. Thus, the iterator for, (*2)

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

Will return, (*3)

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

Current/Next iterator

Allows iteration over an array but returns two items at a time stepping by one item at a time. Thus, the iterator for, (*4)

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

Will return, (*5)

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

Installation

To install it, just include this requirement into your composer.json, (*6)

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

And then run composer install/update as necessary., (*7)

Supports

Only PHP 5.5 or more can be supported due to the fact that key() can only return arrays starting with PHP 5.5!, (*8)

Examples

To use the PreviousCurrentIterator, just instanciate a copy with an array and foreach it!, (*9)

$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 />';
    }
}

To use the CurrentNextIterator, just instanciate a copy with an array and foreach it!, (*10)

$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 />';
        }
    }
}

Use cases

Practical if you need to compare two items together in a previous vs current or current vs next manner., (*11)

The Versions

06/10 2015

dev-master

9999999-dev

Simple package that offers an iterator used for previous vs current comparison

  Sources   Download

MIT

The Development Requires

06/10 2015

1.1.1

1.1.1.0

Simple package that offers an iterator used for previous vs current comparison

  Sources   Download

MIT

The Development Requires

05/10 2015

1.1.0

1.1.0.0

Simple package that offers an iterator used for previous vs current comparison

  Sources   Download

MIT

The Development Requires

02/10 2015

1.0.0

1.0.0.0

Simple package that offers an iterator used for previous vs current comparison

  Sources   Download

MIT

The Development Requires