PHP code example of chipslays / collection

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

    

chipslays / collection example snippets


use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$name = $collection->get('user.name'); // chipslays
$email = $collection->get('user.email', '[email protected]'); // [email protected]

$array = $collection->user; // array('name' => 'chipslays')
$name = $collection->user['name']; // chipslays

$collection = collection([
    'foo' => [
        'bar' => ['baz' => 1],
        'bam' => ['baz' => 2],
        'boo' => ['baz' => 3],
    ],
]);

$collection->get('foo.*.baz');
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
// )

$collection->get('foo.*');
// Array
// (
//     [0] => Array
//         (
//             [baz] => 1
//         )
//     [1] => Array
//         (
//             [baz] => 2
//         )
//     [2] => Array
//         (
//             [baz] => 3
//         )
// )

$collection = collection([
    'foo' => [
        'bar' => ['baz' => 1],
    ],
]);

$collection->get('foo.*.baz');
// 1

$collection->get('foo.*');
// Array
// (
//     [baz] => 1
// )


use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$collection->set('user.name', 'john doe');
$collection->set('user.email', '[email protected]');

$name = $collection->get('user.name'); // john doe
$email = $collection->get('user.email'); // [email protected]

use Chipslays\Collection\Collection;

$collection = new Collection([
    'user' => [
        'name' => 'chipslays',
    ],
]);

$hasName = $collection->has('user.name'); // true
$hasEmail = $collection->has('user.email'); // false

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->first(); // foo

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->last(); // baz

use Chipslays\Collection\Collection;

$collection = new Collection(['foo', 'bar', 'baz']);

echo $collection->shift(); // foo
echo $collection->count(); // 2

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->values()); // collection(green, apple)

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->keys()); // collection(color, name)

use Chipslays\Collection\Collection;

$collection = new Collection(['color' => 'green', 'name' => 'apple']);

print_r($collection->only(['color'])); // collection(color => green)

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

echo $collection->count(); // 10
echo count($collection); // 10

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->clear();

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->toArray();

use Chipslays\Collection\Collection;

$collection = new Collection(range(1, 10));

$collection->toObject();

use Chipslays\Collection\Collection;

$collection = new Collection(['one', 'two']);

echo (string) $collection;

/** output string */
Array
(
    [0] => one
    [1] => two
)