PHP code example of laxity7 / yii2-collection

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

    

laxity7 / yii2-collection example snippets


$collection = new Collection([1, 2, 3]);
echo $collection->map(function($i) { // [2, 3, 4]
    return $i + 1;
})->filter(function($i) { // [2, 3]
    return $i < 4;
})->sum(); // 5

$collection = new Collection([1, 2, 3]);
echo $collection[1]; // 2
foreach($collection as $item) {
    echo $item . ' ';
} // will print 1 2 3

/**
 * {@inheritdoc}
 * @return \yii\db\ActiveQuery|\yii\collection\CollectionBehavior
 */
public static function find()
{
    $query = parent::find();
    $query->attachBehavior('collection', \yii\collection\CollectionBehavior::class);
    return $query;
}