PHP code example of krak / coll

1. Go to this page and download the library: Download krak/coll library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

krak / coll example snippets




use Krak\Coll\Set;

$s1 = new Set\ArraySet();
// or
$s1 = Set\ArraySet::create([1,2,3]);

$s1->add(4);
$s2 = set\union($s1, Set\ArraySet::create([3,4]));

set\is_subset($s2, $s1); // returns true because s1 is a subset of s2

// if you need to store object values

// internally uses SplObjectStorage
$oset = new Set\ObjectSet();
$oset->add(new stdClass());

// if you need to store values that work as array keys
$hset = new Set\HashSet();
$hset->add(['a' => 1]);

// if you need to store any of those types of values
$aset = new Set\AnySet();
set\fill($aset, [1, [1], new stdClass()]);



count($s1); // returns the count
foreach ($s1 as $val) {
    // iterate over the set values
}



use Krak\Coll\Set;

function operate_on_mutable(Set\Set $s1) {
    // ...
}

function operate_on_immutable(Set\ConstSet $s1) {
    // ...
}


$s1->union($s2)
    ->intersect($s3)
    ->difference($s4)
    ->equals($s5);

// const methods
public function get($value);
public function has($value);
public function count();
public function getIterator();

// non-const methods
public function add($value);
public function remove($value);