Skip to content

nspl\ds

Ihor Burlachenko edited this page May 19, 2016 · 1 revision

Provides non-standard data structures and methods to work with them


ArrayObject

Alternative ArrayObject implementation

arrayobject()

Returns new ArrayObject

DefaultArray

Array with a default value for missing keys. If you pass a function as default value it will be called without arguments to provide a default value for the given key, this value will be inserted in the array for the key, and returned. Using DefaultArray turns this code:

$a = array();
foreach([1, 2, 1, 1, 3, 3, 3] as $v) {
    if (!isset($a[$v])) {
        $a[$v] = 0;
    }
    ++$a[$v];
}

into this:

$a = defaultarray(0);
foreach([1, 2, 1, 1, 3, 3, 3] as $v) {
    ++$a[$v];
}
defaultarray($default, $data = array())

Returns new DefaultArray

Set

Array-like collection that contains no duplicate elements. It supports basic set operations which take other sets, arrays and traversable objects as arguments

$set = set(1, 2);

$set->add('hello');
$set[] = 'world';

$set->delete('hello');

$array = [1, 2, 3];
$intersection = $set->intersection($array);

$anotherSet = Set::fromArray([1, 2, 3]);
$difference = $set->difference($anotherSet);

$iterator = new \ArrayIterator([1, 2, 3]);
$union = $set->union($iterator);

$isSubset = $set->isSubset([1, 2, 'hello', 'world']);

$isSuperset = $set->isSuperset([1, 2]);
set

Returns new Set

Check more \nspl\ds examples here.