PHP code example of crazycodr / previous-current-iterator

1. Go to this page and download the library: Download crazycodr/previous-current-iterator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

crazycodr / previous-current-iterator example snippets


['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]

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

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