PHP code example of damijanc / collection

1. Go to this page and download the library: Download damijanc/collection 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/ */

    

damijanc / collection example snippets


use damijanc\Collection\Collection;

$collection = new Collection(['first' => 'value1']);

$collection[] = 'value2';
$collection['data'] = 'value3';

echo $collection['first'];
echo count($collection);

$collection = new Collection([
    'first' => 'Ada',
    10 => 'Grace',
]);

foreach ($collection as $key => $item) {
    // Your logic here.
}

$items = $collection->toArray();

$collection = new Collection();

try {
    $collection['missing'];
} catch (\OutOfBoundsException $exception) {
    // Handle missing key.
}

$collection = new Collection(['optional' => null]);

isset($collection['optional']); // true

final class MyCollection extends Collection
{
    public function add(MyInterface $item): void
    {
        $this->collectionItems[] = $item;
    }
}