Skip to content

Latest commit

 

History

History
112 lines (66 loc) · 1.99 KB

README.template.md

File metadata and controls

112 lines (66 loc) · 1.99 KB

Ar makes working with PHP arrays easy

  • Consistent: All functions accept the array as first parameter.
  • Immutable: the input array is never modified. Fluent style returns a new object for every call.
  • Tested: unit-tested with 100% code coverage.
  • Familiar: function names follow PHP whereever possible.

Fluent style:

use FriendlyPixel\Ar\Ar;

$ints = Ar::wrap([1, 6, 8])
    ->map(fn ($num) => $num * $num)
    ->filter(fn ($value, $key) => $value % 2 == 0);

Functional style:

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)

Install

Install the latest version using Composer:

$ composer require friendly-pixel/ar

Methods

Fluent style only:

Fluent style only methods

wrap

Wrap an array, so you can use fluent syntax to call multiple methods on it. Use ->unwrap() at the end if you need a pure array again.

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()
;

unwrap

Return the underlying array.

use FriendlyPixel\Ar\Ar;
$numbers = Ar::wrap([1, 2, 3])
    ->map(function ($value, $key) { return $value * 2; })
    ->unwrap()
;
// Result: [2, 4, 6]

toArray

Alias for unwrap()

License

MIT license