A PHP library providing common Stack implementation.
{
"require": {
"chroma-x/stack-util": "~1.0"
}
}
require_once('path/to/vendor/autoload.php');
use ChromaX\StackUtil\Stack;
$stack = new Stack();
$stack
->push('Item')
->push('Another item')
->push(12)
->push(null)
->push(8.12);
$stackSize = $stack->size();
echo 'Stack size: ' . $stackSize . PHP_EOL;
Stack size: 5
$lastItem = $stack->get();
echo 'Last item: ' . $lastItem . PHP_EOL;
Last item: 8.12
If the index is not available null
is returned
$secondItem = $stack->get(1);
echo 'Second item: ' . $secondItem . PHP_EOL;
Second item: Another item
$poppedItem = $stack->pop();
echo 'Popped item: ' . $poppedItem . PHP_EOL;
$stackSize = $stack->size();
echo 'Stack size: ' . $stackSize . PHP_EOL;
Popped item: 8.12
Stack size: 4
$lastItem = $stack
->set('9.12')
->get();
echo 'Updated last item: ' . $lastItem . PHP_EOL;
Updated last item: 9.12
$thirdItem = $stack
->set('Third item', 2)
->get(2);
echo 'Updated third item: ' . $thirdItem . PHP_EOL;
Updated third item: Third item
echo 'Stack size: ' . $stack->size() . PHP_EOL;
$stack->delete(1);
echo 'Stack size: ' . $stack->size() . PHP_EOL;
Stack size: 4
Stack size: 3
foreach ($stack as $stackItemKey => $stackItemValue) {
echo 'Stack item ' . $stackItemKey . ': ' . $stackItemValue . PHP_EOL;
}
Stack item 0: Item
Stack item 1: Third item
Stack item 2: 9.12
for ($i = 0, $n = $stack->size(); $i < $n; $i++) {
echo 'Stack index ' . $i . ': ' . $stack->get($i) . PHP_EOL;
}
Stack index 0: Item
Stack index 1: Third item
Stack index 2: 9.12
Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.
PHP Stack Util is under the MIT license.