(Available since version 1.0)
The ArrayDeque class builds on the functionality of SEIDS::Arrays::Dynamic::DynamicArray (which itself builds on SplFixedArray), providing an array that can grow (and shrink) from either end.
The array is grown simply by adding an element to either the beginning or the end of the array.
The API is the same as SplFixedArray but with the following extra methods:
$array
[,
bool
$save_indexes
= true
] )
Example #1 ArrayDeque usage example
<?php
require 'vendor/autoload.php'; // A PSR-4 or PSR-0 autoloader
use SEIDS\Arrays\Dynamic\ArrayDeque;
// Initialize the array with an initial length (optional)
$array = new ArrayDeque(5);
$array[1] = 2;
$array[4] = "foo";
var_dump($array[0]); // NULL
var_dump($array[1]); // int(2)
var_dump($array["4"]); // string(3) "foo"
// Increase the size of the array to 10
$array->setSize(10);
$array[9] = "asdf";
// Shrink the array to a size of 2
$array->setSize(2);
// Add an element to the start
$array->unshift("first");
// Add two elements to the end
$array->push("second to last");
$array[] = "last";
var_dump($array->toArray());
// The following lines throw a RuntimeException: Index invalid or out of range
try {
var_dump($array["non-numeric"]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[-1]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
try {
var_dump($array[5]);
} catch(RuntimeException $re) {
echo "RuntimeException: ".$re->getMessage()."\n";
}
?>
The above example will output:
NULL int(2) string(3) "foo" array(5) { [0] => string(5) "first" [1] => NULL [2] => int(2) [3] => string(14) "second to last" [4] => string(4) "last" } RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range RuntimeException: Index invalid or out of range
The index of the start of the external array within the internal array.
The current array index.