, (*1)
Heap
![Software License][ico-license]
![Total Downloads][ico-downloads], (*2)
Heap is a handy way of interacting with arrays in PHP. This package provides
you with an easy to understand API and a much needed break from the mess
that is PHP's built-in array functions., (*3)
Installation
Install this package via composer:, (*4)
``` bash
$ composer require sven/heap, (*5)
Alternatively, add the package to your dependencies in your `composer.json` file
and run `composer update`:
```json
{
"require": {
"sven/heap": "0.0.2"
}
}
Usage
``` php, (*6)
New up an instance of the class.
$heap = new Sven\Heap\Heap;, (*7)
You can also pass in an array to push to the heap.
$heap = new Sven\Heap\Heap(['foo', 'bar', 'baz']);, (*8)
```php
# Push an item into the heap.
$heap->push('foo');
$heap->all(); // ['foo']
# Merge an existing array into the heap.
$heap->merge(['fizz', 'baz']);
$heap->all(); // ['foo', 'fizz', 'baz']
# Get the first or last item from the heap.
$heap->first(); // 'foo'
$heap->last(); // 'baz'
# Get the first or last `n` items from the heap.
$heap->first(2); // ['foo', 'fizz']
$heap->last(2); // ['fizz', 'baz']
# Pre- or append an item to the heap.
$heap->prepend('bar');
$heap->all(); // ['bar', 'foo', 'fizz', 'baz']
$heap->append('buzz');
$heap->all(); // ['bar', 'foo', 'fizz', 'baz', 'buzz']
$heap->random(); // 'baz' (retrieved randomly)
# Empty the entire array.
$heap->nuke();
$heap->all(); // []
Testing
bash
$ composer test
, (*9)
Security
If you discover any security related issues, please email svenluijten@outlook.com
instead of using the issue tracker., (*10)
Credits
License
The MIT License (MIT). Please see License File for more information., (*11)