PHP Classes

PHP Circular Array: Traverse an array wrapping around when it ends

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 157 All time: 9,007 This week: 206Up
Version License PHP version Categories
circulararray 1.0.0GNU General Publi...5PHP 5, Tools, Data types
Description 

Author

This class can traverse an array wrapping around when it ends.

It takes a given array of elements and can iterate over the elements without ending.

When the class reaches the last element it goes back to the first, or it goes back to the last when going backwards and reaching the first element.

The class can reset the position of iteration to the first element, move to a specific position, or move to a position relative to the current.

Picture of Alexandre Sinício
  Performance   Level  
Name: Alexandre Sinício <contact>
Classes: 7 packages by
Country: Brazil Brazil
Innovation award
Innovation award
Nominee: 3x

Example

<?php

use alesinicio\circularArray;

require
"circularArray.php";

// LET'S CREATE AN EXAMPLE ARRAY AND CONVERT IT INTO A CIRCULAR ARRAY.
$arrExample = [1,2,3,4];
$arrCircular = new circularArray($arrExample);

// GO THROUGH A LOOP. EACH ITERATION WILL PRINT THE CURRENT VALUE
// AND ADVANCE TO THE NEXT POSITION OF THE CIRCULAR ARRAY.
for ($i=0; $i<10; $i++) {
    echo
$arrCircular->getCurrentValue();
   
$arrCircular->next();
}

echo
"<hr>";

// RESET THE ARRAY TO THE INITIAL POSITION.
$arrCircular->reset();

// GO THROUGH A LOOP. EACH ITERATION WILL PRINT THE CURRENT VALUE
// AND REWIND TO THE PREVIOUS POSITION OF THE CIRCULAR ARRAY.
// NOTE THAT HERE WE USE THE getCurrentValueAndRewind() METHOD,
// WHICH IS A WRAPPER FOR getCurrentValue() + previous().
for ($i=0; $i<10; $i++) {
    echo
$arrCircular->getCurrentValueAndRewind();
}

echo
"<hr>";

// WE CAN ALSO ADVANCE/REWIND "N" POSITIONS ON THE ARRAY
$arrCircular->reset();
echo
$arrCircular->advancePosition(2);
echo
$arrCircular->rewindsPosition(2);

echo
"<hr>";

// MAYBE YOU WANT TO KNOW THE CURRENT INDEX OF THE ARRAY, NOT IT'S VALUE. COOL.
echo $arrCircular->getCurrentIndex();


Details

circularArray

Simple PHP class to convert conventional arrays into circular arrays.

Sometimes we need to use arrays in a non-conventional way, in a circular manner where the array wraps around itself. Although it is not particularly difficult to implement it in your own code, it may be even simpler to use something that is already done, functional and tested.

<h2>Indexed and associative arrays</h2> You can use both indexed and associative arrays. When you use associative arrays, this class WILL NOT make any kind of sorting.

<h2>Methods</h2> There are probably enough methods to meet your needs. You can create the circular array, get the value of the current position, get the index of the current position, advance, rewind and reset the pointer. There are also some methods that wrap two methods in one call, like "advance and give me the new value".

<h2>Easy to use</h2> Example file is provided. The basic usage is:

$arrExample = [1,2,3,4];
$arrCircular = new circularArray($arrExample);

for ($i=0; $i<10; $i++) {
	echo $arrCircular->getCurrentValue();
	$arrCircular->next();
}

The expected output is "1234123412" (walk 10 times through the "1,2,3,4" array).


  Files folder image Files (3)  
File Role Description
Plain text file circularArray.php Class Class source
Accessible without login Plain text file example.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:157
This week:0
All time:9,007
This week:206Up