PHP code example of gears / arrays

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

    

gears / arrays example snippets


$data = [];

$data = Gears\Arrays\add($data, 'a.b.c', 'd');

// $data now looks like: ['a' => ['b' => ['c' => 'd']]];

Gears\Arrays\set($data, 'a.b.c', 'foo');

// $data now looks like: ['a' => ['b' => ['c' => 'foo']]];

Gears\Arrays\forget($data, 'a.b.c');

// $data now looks like: ['a' => ['b' => []]];

// Import the functions
use function Gears\Arrays\add;
use function Gears\Arrays\set;
use function Gears\Arrays\forget;

// This results in the same array
$data = [];
$data = add($data, 'a.b.c', 'd');
set($data, 'a.b.c', 'foo');
forget($data, 'a.b.c');

// Import the Array class
use Gears\Arrays as Arr;

// This results in the same array
$data = [];
$data = Arr::add($data, 'a.b.c', 'd');
Arr::set($data, 'a.b.c', 'foo');
Arr::forget($data, 'a.b.c');

$data = new Gears\Arrays\Fluent();
$data[] = 'foo';
$data[] = 'bar';

foreach ($data as $item)
{
	echo $item.',';
}

// you would see: foo,bar

$real_array = $data->toArray();

// Import the Array class
use Gears\Arrays as Arr;

// Use the factory method
$data = Arr::a();

$data = Arr::a();
$data->add('a.b.c', 'd');
$data->set('a.b.c', 'foo');
$data->forget('a.b.c');

$data = $data->add('a.b.c', 'd');

Arr::a([1,2,3])->add(4)->each(function($v){ echo $v.','; });

// would output: 1,2,3,4,

$data = Arr::a(['a' => ['b' => ['c' => 'd']]]);

print_r($data);

// you would see something like:
Gears\Arrays\Fluent Object
(
	[items:protected] => Array
	(
		[a] => Array
		(
			[b] => Array
			(
				[c] => 'd'
			)
		)
	)
)

// now lets access something
$data['a']['b']['c'];

print_r($data);

// you would now see something like:
Gears\Arrays\Fluent Object
(
	[items:protected] => Array
	(
		[a] => Gears\Arrays\Fluent Object
		(
			[items:protected] => Array
			(
				[b] => Gears\Arrays\Fluent Object
				(
					[items:protected] => Array
					(
						[c] => d
					)
				)
			)
		)
	)
)

$data1 = Arr::a();
$data1['a'] = [];
$data1['a']['b'] = [];
$data1['a']['b']['c'] = 'd';

$data2 = Arr::a();
$data2->a = [];
$data2->a->b = [];
$data2->a->b->c = 'd';

// $data1 == $data2

'Arr' => 'Gears\Arrays',
/app/config/app.php