PHP code example of dezworkastronphp / collection

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

    

dezworkastronphp / collection example snippets


public function __construct($content = [])


use Astronphp\Collection\Collection;

$collection1 = new Collection();
$collection2 = new Collection(['lorem' => 'ipsum']);
$collection3 = new Collection(new \DateTime('now'));

var_dump($collection1, $collection2, $collection3);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => array (size = 0)
  protected 'length'  => int 0

object(Astronphp\Collection\Collection)[2]
  protected 'content' => array (size=1)
    'lorem' => string 'ipsum' (length=5)
  protected 'length' => int 1

object(Astronphp\Collection\Collection)[4]
  protected 'content' => array (size=3)
      'date' => string '2019-09-17 14:42:47.000000' (length=26)
      'timezone_type' => int 3
      'timezone' => string 'UTC' (length=3)
  protected 'length' => int 3
*/


public function unshift(...$values): self

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->unshift('lorem');
$collection->unshift('ipsum', 'dolor');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=3)
      0 => string 'ipsum' (length=5)
      1 => string 'dolor' (length=5)
      2 => string 'lorem' (length=5)
  protected 'length' => int 3
*/

public function push(...$values): self

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->push('lorem');
$collection->push('ipsum', 'dolor');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=3)
      0 => string 'lorem' (length=5)
      1 => string 'ipsum' (length=5)
      2 => string 'dolor' (length=5)
  protected 'length' => int 3
*/


public function set(string $key, $value): self

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');
$collection->set('dolor.amet', 'consectetur');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'lorem' => string 'ipsum' (length=5)
      'dolor' => 
        array (size=1)
          'amet' => string 'consectetur' (length=11)
  protected 'length' => int 2
*/

public function get(string $key)

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');
$collection->set('dolor.amet', 'consectetur');

$collection->get('lorem'); // ipsum
$collection->get('dolor.amet'); // consectetur


public function isset(string $key): bool

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');

$collection->isset('lorem'); // true
$collection->isset('dolor'); // false


public function empty(string $key): bool

use Astronphp\Collection\Collection;

$collection = new Collection();

$collection->set('lorem', 1);
$collection->set('ipsum', 0);

$collection->empty('lorem'); // false
$collection->empty('ipsum'); // true


public function unset(string $key)

use Astronphp\Collection\Collection;

$collection = new Collection();
$collection->set('lorem', 'ipsum');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=1)
      'lorem' => string 'ipsum' (length=5)
  protected 'length' => int 1
*/

$collection->unset('lorem');

var_dump($collection);

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=0)
      empty
  protected 'length' => int 0
*/


public function length(): int

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->length(); // 5


public function shift()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->shift(); // 1


public function pop()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->pop(); // 5


public function first()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->first(); // 1


public function last()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->last(); // 5


public function each(callable $callback)

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
$collection->each(function($key, $value) {
    var_dump($key, $value);
});

/*
string 'lorem' (length=5)
string 'ipsum' (length=5)

string 'dolor' (length=5)
string 'amet' (length=4)
*/


public function for(int $start, int $step, callable $callback)

use Astronphp\Collection\Collection;

$collection = new Collection([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
$collection->for(0, 2, function($key, $value) {
    var_dump($value);
});

/* int 0, int 2, int 4, int 6, int 8, int 10 */


public function walk(callable $callback, $type = \RecursiveIteratorIterator::LEAVES_ONLY)

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', ['ipsum', 'dolor'], ['sit' => ['amet' => 'consectetur']]]);
$collection->walk(function($key, $value) {
    var_dump($key, $value);
});

/*
int 0
string 'lorem' (length=5)
int 0
string 'ipsum' (length=5)
int 1
string 'dolor' (length=5)
string 'amet' (length=4)
string 'consectetur' (length=11)
*/


public function sum()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->sum(); // 15


public function contains($value): bool

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
$collecion->contains('dolor'); // true


public function map(callable $callback): self

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
var_dump($collection->map(function($key, $value) {
    return [$key => strtoupper($value)];
}));

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=3)
      0 => string 'LOREM' (length=5)
      1 => string 'IPSUM' (length=5)
      2 => string 'DOLOR' (length=5)
  protected 'length' => int 3
*/


public function filter(callable $callback): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var_dump($collection->filter(function($key, $value) {
    return $value > 5;
}));

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=4)
      5 => int 6
      6 => int 7
      7 => int 8
      8 => int 9
  protected 'length' => int 4
*/


public function reduce(callable $callback): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$result     = $collection->reduce(function($a, $b) {
    return $a + $b;
});

var_dump($result); // int 15


public function join(string $glue)

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->join('-')); // string '1-2-3-4-5'


public function random(int $num = 1)

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->random(); // 3


public function shuffle(): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->shuffle());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 3
      1 => int 1
      2 => int 5
      3 => int 2
      4 => int 4
  protected 'length' => int 5
*/


public function flip(): self

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem', 'ipsum', 'dolor']);
var_dump($collection->flip());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      'lorem' => int 0
      'ipsum' => int 1
      'dolor' => int 2
  protected 'length' => int 3
*/


public function keys(): self

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->keys());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'lorem' (length=5)
      1 => string 'dolor' (length=5)
  protected 'length' => int 2
*/


public function values(): self

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor', 'amet']);
var_dump($collection->values());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'ipsum' (length=5)
      1 => string 'amet' (length=4)
  protected 'length' => int 2
*/


public function column($key, $index = null)

use Astronphp\Collection\Collection;

$collection = new Collection([
    [
        'lorem' => 'ipsum',
        'dolor' => 'amet',
    ],
    [
        'lorem' => 'dolor',
        'dolor' => 'consectetur',
    ],
]);

var_dump($collection->column('lorem'));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=2)
      0 => string 'ipsum' (length=5)
      1 => string 'dolor' (length=5)
  protected 'length' => int 2
*/


public function chunk(int $size, bool $preserve_keys = false): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var_dump($collection->chunk(3));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      0 => 
        array (size=3)
          0 => int 1
          1 => int 2
          2 => int 3
      1 => 
        array (size=3)
          0 => int 4
          1 => int 5
          2 => int 6
      2 => 
        array (size=3)
          0 => int 7
          1 => int 8
          2 => int 9
  protected 'length' => int 3
*/


public function unique(int $flags = SORT_STRING): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 1, 2, 3, 4, 5, 2, 3, 4]);
var_dump($collection->unique());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 1
      1 => int 2
      2 => int 3
      6 => int 4
      7 => int 5
  protected 'length' => int 5
*/


public function coalesce()

use Astronphp\Collection\Collection;

$collection = new Collection([null, null, null, 'lorem', null, null]);
var_dump($collection->coalesce()); // string 'lorem'


public function merge();

use Astronphp\Collection\Collection;

$collection = new Collection([
    ['lorem' => 'ipsum'],
    ['dolor' => 'sit'],
    ['amet'  => 'consectetur'],
]);

var_dump($collection->merge());

/*
object(Astronphp\Collection\Collection)[4]
  protected 'content' => 
    array (size=3)
      'lorem' => string 'ipsum' (length=5)
      'dolor' => string 'sit' (length=3)
      'amet' => string 'consectetur' (length=11)
  protected 'length' => int 3
*/


public function reverse($preserve_keys = null): self

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->reverse());

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=5)
      0 => int 5
      1 => int 4
      2 => int 3
      3 => int 2
      4 => int 1
  protected 'length' => int 5
*/


public function search($value, bool $strict = null)

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->search('ipsum')); // 'lorem'


public function lower(): self

use Astronphp\Collection\Collection;

$collection = new Collection(['Lorem' => 'Ipsum', 'Dolor' => 'Amet']);
var_dump($collection->upper());

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'LOREM' => string 'Ipsum' (length=5)
      'DOLOR' => string 'Amet' (length=4)
  protected 'length' => int 2
*/


public function upper(): self

use Astronphp\Collection\Collection;

$collection = new Collection(['Lorem' => 'Ipsum', 'Dolor' => 'Amet']);
var_dump($collection->lower());

/*
object(Astronphp\Collection\Collection)[3]
  protected 'content' => 
    array (size=2)
      'lorem' => string 'Ipsum' (length=5)
      'dolor' => string 'Amet' (length=4)
  protected 'length' => int 2
*/


public function toArray()

use Astronphp\Collection\Collection;

$collection = new Collection([1, 2, 3, 4, 5]);
var_dump($collection->toArray());

/*
array (size=5)
  0 => int 1
  1 => int 2
  2 => int 3
  3 => int 4
  4 => int 5
*/


public function toJson()

use Astronphp\Collection\Collection;

$collection = new Collection(['lorem' => 'ipsum', 'dolor' => 'amet']);
var_dump($collection->toJson()); // string '{"lorem":"ipsum","dolor":"amet"}'


public function sort()

use Astronphp\Collection\Collection;

$collection = new Collection([3, 4, 8, 7, 1, 5]);
$collection->sort(); //[1,3,4,5,7,8]


public function rsort()

use Astronphp\Collection\Collection;

$collection = new Collection([3, 4, 8, 7, 1, 5]);
$collection->rsort(); //[8,7,5,4,3,1]


public function asort()

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->asort(); //["dolor" => "amet","sit" => "consectetur","lorem" => "ipsum"]


public function arsort()

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->arsort(); // ["lorem" => "ipsum","sit" => "consectetur","dolor" => "amet"]


public function ksort()

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->ksort(); //["dolor" => "amet","lorem" => "ipsum","sit" => "consectetur"]


public function krsort()

use Astronphp\Collection\Collection;

$collection = new Collection([
    'lorem' => 'ipsum',
    'dolor' => 'amet',
    'sit' => 'consectetur'
]);
$collection->krsort(); //["sit" => "consectetur","lorem" => "ipsum","dolor" => "amet"]


public function union()

use Astronphp\Collection\Collection;

$collection = new Collection([
    [1, 2, 3],
    [3, 4, 5],
]);

$collection->union(); // [1, 2, 3, 4, 5]


public function diff()

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->diff(); // [1, 2]

$collection = new Collection([
   [3, 4, 5],
   [1, 2, 3],
]);



public function outer()

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->outer(); // [[1, 2], [4, 5]]


public function intersect()

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3, 4],
   [3, 4, 5, 6],
]);

$collection->intersect(); // [3, 4]


public function cartesian()

use Astronphp\Collection\Collection;

$collection = new Collection([
   [1, 2, 3],
   [3, 4, 5],
]);

$collection->cartesian(); // [[1,3], [1,4], [1,5], [2,3], [2,4], [2,5], [3,3], [3,4], [3,5]]


public static function isCollection(): bool

use Astronphp\Collection\Collection;

Collection::isCollection(new Collection()); // true

Collection::isCollection(10); // false

php
public function combine(): self
php
use Astronphp\Collection\Collection;

$array = ['lorem', 'ipsum', 'dolor'];

$collection = new Collection([1, 2, 3]);

var_dump(Collection::combine($array, $collection));

/*
object(Astronphp\Collection\Collection)[2]
  protected 'content' => 
    array (size=3)
      'lorem' => int 1
      'ipsum' => int 2
      'dolor' => int 3
  protected 'length' => int 3
*/

php
public static function range($start, $end, $step = 1): self