PHP code example of bluzphp / collection

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

    

bluzphp / collection example snippets


$arr = [
    'a', 
    'b' => 'boo',
    'c' => [
        'c1',
        'c2',
        'c3'
    ],
    'd' => [
        'd1' => ['d1.1', 'd1.2'],
        'd2' => ['d2.1', 'd2.2'],
        'd3' => ['d3.1', 'd3.2'],
    ],
    'e' => null,
    'f' => false
];

Collection::has(array $array, ...$keys);
array_has($array, ...$keys);

array_has($array, 0);         // true 
array_has($array, 'b');       // true
array_has($array, 'c', 0);    // true
array_has($array, 'd', 'd1'); // true
array_has($array, 'e');       // true
array_has($array, 'f');       // true
array_has($array, 'g');       // false

isset($array['e']); // false

Collection::get(array $array, ...$keys);
array_get(array $array, ...$keys);

array_get($array, 0);         // 'a' 
array_get($array, 'b');       // 'boo'
array_get($array, 'c', 0);    // 'c1'
array_get($array, 'd', 'd1'); // ['d1.1', 'd1.2']
array_get($array, 'e');       // null
array_get($array, 'e', 'e');  // null

Collection::add(array &$array, $key, ...$values);
array_add(array &$array, $key, ...$values);

array_add($array, 'c', 'c3');           // $array['c'][] = 'c3'
array_add($array, 'd', 'd1', 'd1.3');   // $array['d']['d1'][] = 'd1.3'
array_add($array, 'g', 'g1', 'g1.1');   // $array['g']['g1'][] = 'g1.1'

// but
array_add($array, 'b', 'b1');           // InvalidArgumentException - $array['b'] is not an array

Collection::set(array &$array, $key, ...$values);
array_set(array &$array, $key, ...$values);

array_set($array, 'g', 'game over');       // $array['g'] = 'game over';
array_set($array, 'h', 'high way', 'e95'); // $array['h']['high way'] = 'e95';