Skip to content

Latest commit

 

History

History
25 lines (19 loc) · 1.18 KB

README.md

File metadata and controls

25 lines (19 loc) · 1.18 KB

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.

Indexed and associative arrays

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

Methods

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

Easy to use

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