PHP code example of pisc / arrr
1. Go to this page and download the library: Download pisc/arrr 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/ */
pisc / arrr example snippets
// shorthand function (global function)
$array = ar([ 'someKey' => 'someValue' ]);
// constructor
$array = new Arrr([ 'someKey' => 'someValue' ]);
$array = new Ar([ 'someKey' => 'someValue' ]); // returns Ar instance
$array->mapIt(function($item) {
return $item . "_suffix";
}); // returns Arrr instance
// factory
$array = Arrr::ar([ 'someKey' => 'someValue' ]);
$array = Arrr::instance([ 'someKey' => 'someValue' ]);
$array = Ar::ar([ 'someKey' => 'someValue' ]);
$array = Ar::instance([ 'someKey' => 'someValue' ]);
use pisc\arrr\arrr;
$array = ar([ 'someKey' => 'someValue' ]);
$flat = Ar::flatten([ 'cow', [ 'bear', ['bunny', 'santa' ], 'rabbit' ]]);
$places = ar([
'Amsterdam',
'Berlin',
'Paris',
'Vienna',
'Rome',
'Madrid'
]);
$places->filterIt(function($place) {
return in_array($place[0], [ 'A', 'V', 'R' ]);
});
$places->mapIt(function($place) {
return "To destination {$place}";
})
echo $places->toJson();
// returns
'[ "To destination Amsterdam", "To destination Vienna", "To destination Rome" ]'
$array = ar([
(object)[ 'id' => 1, 'name' => 'Jack', 'length' => 180 ],
(object)[ 'id' => 2, 'name' => 'Jack', 'length' => 150 ],
(object)[ 'id' => 3, 'name' => 'Ben', 'length' => 180 ],
(object)[ 'id' => 4, 'name' => 'Ben', 'length' => 150 ],
(object)[ 'id' => 5, 'name' => 'Vince', 'length' => 180 ],
(object)[ 'id' => 6, 'name' => 'Vince', 'length' => 150 ],
]);
$sortedArray = $array->sortBy([ 'length', 'name' ], [ 'Sort::byDefault', 'strcmp' ]);
// result
[
(object)[ 'id' => 4, 'name' => 'Ben', 'length' => 150 ],
(object)[ 'id' => 2, 'name' => 'Jack', 'length' => 150 ],
(object)[ 'id' => 6, 'name' => 'Vince', 'length' => 150 ],
(object)[ 'id' => 3, 'name' => 'Ben', 'length' => 180 ],
(object)[ 'id' => 1, 'name' => 'Jack', 'length' => 180 ],
(object)[ 'id' => 5, 'name' => 'Vince', 'length' => 180 ],
]