PHP code example of friendly-pixel / ar
1. Go to this page and download the library: Download friendly-pixel/ar 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/ */
friendly-pixel / ar example snippets
use FriendlyPixel\Ar\Ar;
$ints = Ar::wrap([1, 6, 8])
->map(fn ($num) => $num * $num)
->filter(fn ($value, $key) => $value % 2 == 0);
use FriendlyPixel\Ar\Ar;
$ints = [1, 5, 8];
$ints = Ar::map($ints, fn($num) => $num * $num);
$ints = Ar::filter($ints, fn($value, $key) => $value % 2 == 0)
use FriendlyPixel\Ar\Ar;
$count = Ar::count([1, 2, 3]);
$count = Ar::wrap([1, 2, 3])
->count()
;
// Result: 3
use FriendlyPixel\Ar\Ar;
$even = Ar::filter([1, 2, 3, 12], function($value, $key) { return $value % 2 == 0; });
$even = Ar::wrap([1, 2, 3, 12])
->filter(function($value, $key) { return $value % 2 == 0; })
->unwrap();
// Result: [1 => 2, 3 => 12]
use FriendlyPixel\Ar\Ar;
Ar::first([2, 3, 4]);
Ar::wrap([2, 3, 4])->first();
// Result: 2
use FriendlyPixel\Ar\Ar;
$result = Ar::implode(['a', 'b', 'c'], ',');
$result = Ar::wrap(['a', 'b', 'c'])
->implode(',')
;
// result: "a,b,c"
use FriendlyPixel\Ar\Ar;
$result = Ar::keys([3 => 'a', 'foo' => 'b', 1 => 'c']);
$result = Ar::wrap([3 => 'a', 'foo' => 'b', 1 => 'c'])->keys()->unwrap();
// result: [3, 'foo', 1]
use FriendlyPixel\Ar\Ar;
Ar::last([2, 3, 4]);
Ar::wrap([2, 3, 4])->last();
// Result: 4
use FriendlyPixel\Ar\Ar;
$numbers = Ar::map([1, 2, 3], function($value, $key) { return $value * 2; });
$numbers = Ar::wrap([1, 2, 3])
->map(function($value, $key) { return $value * 2; })
->unwrap();
// Result: [2, 4, 6]
use FriendlyPixel\Ar\Ar;
$numbers = Ar::mapKeys([1, 2, 3], function($value, $key) { return $key * 2; });
$numbers = Ar::wrap([1, 2, 3])
->mapKeys(function($value, $key) { return $key * 2; })
->unwrap();
// Result: [0 => 2, 2 => 2, 4 => 3]
use FriendlyPixel\Ar\Ar;
$numbers = Ar::merge(['a', 'b'], ['c', 'd']));
$numbers = Ar::wrap(['a', 'b'])
->merge(['b', 'c'])
->unwrap();
// Result:['a', 'b', 'c', 'd']
use FriendlyPixel\Ar\Ar;
$result = Ar::push([1, 2], 3, 4);
$result = Ar::wrap([1, 2])->push(3, 4)->unwrap();
// result: [1, 2, 3, 4]
use FriendlyPixel\Ar\Ar;
$found = Ar::search([ ['a' => 1], ['a' => 8], ['a' => 3] ], function($value, $key) { return $value['a'] == 3; });
$found = Ar::wrap([ ['a' => 1], [], ['a' => 3] ])
->search(function($value, $key) { return $value['a'] == 3; })
;
// Result: ['a' => 3]
use FriendlyPixel\Ar\Ar;
$even = Ar::slice(['a', 'b', 'c', 'd'], 1, 2);
$even = Ar::wrap(['a', 'b', 'c', 'd'])
->slice(1, 2)
->unwrap();
// Result: ['b', 'c']
use FriendlyPixel\Ar\Ar;
$even = Ar::splice(['a', 'b', 'c', 'd'], 1, 1, ['q', 'x']);
$even = Ar::wrap(['a', 'b', 'c', 'd'])
->splice(1, 1, ['q', 'x'])
->unwrap();
// Result: ['a', 'q', 'x', 'c', 'd']
use FriendlyPixel\Ar\Ar;
$result = Ar::unique(['a', 'a', 'b']);
$result = Ar::wrap(['b', 4])->unique(['a', 'a', 'b'])->unwrap();
// result: [0 => 'a', 2 => 'b']
use FriendlyPixel\Ar\Ar;
$result = Ar::unshift([3, 4], 1, 2);
$result = Ar::wrap([3, 4])->unshift(1, 2)->unwrap();
// result: [1, 2, 3, 4]
use FriendlyPixel\Ar\Ar;
$result = Ar::values([3 => 'a', 'foo' => 'b', 1 => 'c']);
$result = Ar::wrap([3 => 'a', 'foo' => 'b', 1 => 'c'])->values()->unwrap();
// result: [0 => 'a', 1 => 'b', 2 => 'c']
use FriendlyPixel\Ar\Ar;
$numbers = Ar::wrap([1, 2, 3])
->map(function ($value, $key) { return $value * 2; })
->filter(function ($value) { return $value != 6; })
->unwrap()
;
// If you don't like the Ar::wrap syntax, you can also use ArFluent directly:
use FriendlyPixel\Ar\ArFluent;
$numbers = (new ArFluent([1, 2, 3]))
->map(function ($value, $key) { return $value * 2; })
->filter(function ($value) { return $value != 6; })
->unwrap()
;
use FriendlyPixel\Ar\Ar;
$numbers = Ar::wrap([1, 2, 3])
->map(function ($value, $key) { return $value * 2; })
->unwrap()
;
// Result: [2, 4, 6]