Consistent array functions


Keywords
arrays, php, php-arrays
License
MIT

Documentation

Ar - PHP array utility functions

Consistent and (optionally) fluent map, reduce etc. for PHP arrays.

  • All functional style functions accept the array as first parameter.
  • All user-supplied callables get $value, $key as parameters.
  • Immutable: the input array is never modified. Fluent style returns a new object for every call.

Functional style:

use Frontwise\Ar\Ar;

$ints = [1, 5, 8];
$ints = Ar::map($ints, function($value, $key) { return $value * $value; });
$ints = Ar::filter($ints, function($value, $key) { return $value % 2 == 0; })

Fluent style:

use Frontwise\Ar\Ar;

$ints = Ar::new([1, 5, 8])
    ->map(function($value, $key) { return $value * $value; })
    ->filter(function($value, $key) { return $value % 2 == 0; })
    ->unwrap()
;

Install

Install the latest version using Composer:

$ composer require frontwise/ar

Methods

Fluent style only:

filter

Only return items that match.

Pass every item into a user-supplied callable, and only put the item into the result array if the returned value is true. Keys are preserved.

// Functional
use Frontwise\Ar\Ar;

$even = Ar::filter([1, 2, 3], function($value, $key) { return $value % 2 == 0; }); 

// Result: [0 => 2, 2 => 2, 4 => 3]
// Fluent
use Frontwise\Ar\Ar;

$even = Ar::new([1, 2, 3])
    ->filter(function($value, $key) { return $value % 2 == 0; })
    ->unwrap()
;

map

Transform values.

Pass every item into a user-supplied callable, and put the returned value into the result array. Keys are preserved.

// Functional
use Frontwise\Ar\Ar;

$numbers = Ar::map([1, 2, 3], function($value, $key) { return $value * 2; }); 

// Result: [2, 4, 6]
// Fluent
use Frontwise\Ar\Ar;

$numbers = Ar::new([1, 2, 3])
    ->map(function ($value, $key) { return $value * 2; })
    ->unwrap()
;

mapKeys

Transform keys.

Pass every item and key into a user-supplied callable, and use the returned value as key in the result array.

// Functional
use Frontwise\Ar\Ar;

$numbers = Ar::mapKeys([1, 2, 3], function($value, $key) { return $key * 2; }); 

// Result: [0 => 2, 2 => 2, 4 => 3]
// Fluent
use Frontwise\Ar\Ar;

$numbers = Ar::new([1, 2, 3])
    ->mapKeys(function($value, $key) { return $key * 2; })
    ->unwrap()
;

search

Return the first value for which the callable returns true. Returns null otherwise.

// Functional
use Frontwise\Ar\Ar;

$found = Ar::search([ ['a' => 1], ['a' => 8], ['a' => 3] ], function($value, $key) { return $value['a'] == 3; }); 

// Result: ['a' => 3]
// Fluent
use Frontwise\Ar\Ar;

$found = Ar::new([ ['a' => 1], [], ['a' => 3] ])
    ->search(function($value, $key) { return $value['a'] == 3; })
;

Fluent style only methods

new

(When using fluent style): Create a new ArFluent object wrapping the array.

// Fluent
use Frontwise\Ar\Ar;

$numbers = Ar::new([1, 2, 3])
    ->map(function ($value, $key) { return $value * 2; })
;

// If you don't like the Ar::new syntax, you can also use ArFluent directly:
use Frontwise\Ar\ArFluent;

$numbers = (new ArFluent([1, 2, 3]))
    ->map(function ($value, $key) { return $value * 2; })
;

unwrap

(When using fluent style): get the underlying array back.

// Fluent
use Frontwise\Ar\Ar;

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

implode

(When using fluent style): Join all items into a big string, using $glue as separator.

// Fluent
use Frontwise\Ar\Ar;

$numbers = Ar::new([1, 2, 3])
    ->map(function ($value, $key) { return $value * 2; })
    ->join(' - ')
;
// Result: "1 - 2 - 3"

License

MIT license