PHP code example of irfantoor / collection

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

    

irfantoor / collection example snippets




use IrfanTOOR\Collection;

$init = [
	'app' => [
		'name' => 'My App',
		'version' => '1.1',
	],
	'debug' => [
		'level' => 1,
		'log'   => [
			'enabled' => 0,
			'channel' => 'DEBUG',
			'file'    => '/tmp/debug.log',
		],
	]
];

$app = new IrfanTOOR\Collection($init);

use IrfanTOOR\Collection;

$app = new IrfanTOOR\Collection();

# setting hello => world
$app->set('hello', 'world');

# using an array notation
$app['hello'] = 'world';

# defining multiple
$app->setMultiple([
  'hello'     => 'world',
  'box'       => 'empty',
  'something' => null,
  'array'     => [
    'action'       => 'go',
    'step'         => 1,
  ],
]);

# defining sub values
$app->set('app.name', 'Another App');
$app->set('debug.level', 2);

$app['hello'] = 'world';
$app['debug.log.file'] = '/my/debug/log/file.log';

$debug_level = $app->get('debug.level');

# returns the value stored against this identifier or returns 0 if the identifier
# is not present in the collection
$debug_log_level = $app->get('debug.log.level', 0);

$debug_level = $app['debug.level'];

# returns the value stored against this identifier or returns 0 if the identifier
# is not present in the collection
$debug_log_level = $app['debug.log.level'] ?? 0;

if ($app->has('debug.level')) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'debug.level is present in the collection'
}

if (isset($app['debug.level']) {
  # this will be executed even if the given identifier has the value NULL, 0
  # or false
  echo 'debug.level is present in the collection'
}

# using method remove
$app->remove('debug.level');

# using unset
unset($app['debug.level']);

$array = $app->toArray();

$ids_array = $app->keys();

# will return 2 for the Collection defined in initialization section
$count = $app->count();

foreach($app->toArray() as $k => $v)
{
    print_r([$k => $v]);
}

foreach($app as $k => $v)
{
    print_r([$k => $v]);
}

$config = new IrfanTOOR\Collection([
  'app' => [
    'name' => 'My App',
    'version' => '1.1',
  ],
  'debug' => [
    'level' => 1,
    'log'   => [
      'enabled' => 0,
      'channel' => 'DEBUG',
      'file'    => '/tmp/debug.log',
    ],
  ]
]);

$config->lock();

# will not modify the values
$config->set('debug.level', 0);
$config->set('app.version', '1.2');

# will not remove the value
$config->remove('debug.log');

  $c = new Collection(['10', 1, 2, 3, 4]);

  $callback = function ($key, $value) {
    return is_int($value);
  };

  $int = $c->filter($callback); # 1, 2, 3, 4

  $callback = function ($key, $value) {
    return $value * $value;
  };

  # int is the collection from previous example
  $int2 = $int->map($callback); # 1, 4, 9, 16

  $callback = function ($key, $value, $carry) {
    return $carry + $value;
  };

  $sum = $int2->reduce($callback); # 30