Sorry, the function I meant to post was array_unshift. This prepends an element to the front of an array. The type of FIFO you are asking for is a pipe as a normal FIFO structure grows in size similar to how a queue inside a shop grows.
If you were to describe this pipe using an object, you could have something like this:
PHP Code:
class Pipe
{
private $pipeArray;
private $size;
private $pointer = 0;
public function __construct($size)
{
$this->size = (int) $size;
$this->pipeArray = Array($this->size);
}
/**
* Adds data to the pipe. If the pipe has reached its maximum
* size the first element in the pipe is returned and removed from the
* pipe to make room for the new element.
*/
public function push($data)
{
$first = null;
if ($this->pointer == $this->size - 1) { // pipe is full
$first = array_pop($this->pipeArray);
} else {
++$this->pointer;
}
array_unshift($this->pipeArray, $data); // add the data
return $first;
}
/**
* Pulls the first element from the pipe, reducing its contents by 1.
*/
public function pop()
{
$data = null;
if ($this->pointer != 0) {
--$this->pointer;
$data = array_pop($this->pipeArray);
}
return $data;
}
/**
* Returns the current pipe as an array.
*/
public function get_pipe_as_array()
{
return array_reverse($this->pipeArray);
}
}
The object can then be used as follows:
PHP Code:
$pipe = new Pipe(3); // makes a pipe containing 3 elements
$pipe->push(100);
$pipe->push(200);
$pipe->push(300); // pipe should now be full
$pipe->push(400);
print_r($pipe->get_pipe_as_array());
Note, that the array is returned in reverse order, so you can traverse it using a foreach loop and it will maintain the FIFO nature of the pipe.