PHP Classes

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

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 156 All time: 8,966 This week: 491Up
Version License PHP version Categories
circulararray 1.0.0GNU General Publi...5PHP 5, Utilities and 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
Age: 38
All time rank: 2103137 in Brazil Brazil
Week rank: 703 Up47 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

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  
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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:156
This week:0
All time:8,966
This week:491Up