A comprehensive collection of functional array utilities for PHP that provides the array functions you wish were part of the standard library. This package offers a consistent, functional programming approach to array manipulation with support for both arrays and iterables.
composer require recruiterphp/array- Functional programming style - All functions follow functional programming principles
- Iterable support - Most functions work with both arrays and
Traversableobjects - Consistent API - Predictable function signatures and behaviors
- Zero dependencies - Only requires PHP 8.4+
- Well-tested - Comprehensive test coverage with PHPUnit
Enhanced version of PHP's array_reduce that supports any traversable and preserves keys.
use function Recruiter\Array\array_reduce;
$sum = array_reduce([1, 2, 3], fn($acc, $val) => $acc + $val, 0); // 6Maps over arrays with optional key preservation and index access.
use function Recruiter\Array\array_map;
$doubled = array_map([1, 2, 3], fn($n) => $n * 2); // [2, 4, 6]
// With key preservation
$result = array_map(['a' => 1, 'b' => 2], fn($v) => $v * 2, true);
// ['a' => 2, 'b' => 4]Concatenates arrays and scalar values into a single array.
use function Recruiter\Array\array_concat;
$result = array_concat(1, [2, 3], [4, 5]); // [1, 2, 3, 4, 5]Recursively merges arrays. Unlike PHP's array_merge_recursive, this keeps the second value when merging non-array values.
use function Recruiter\Array\array_merge;
$merged = array_merge(
['a' => ['x' => 1]],
['a' => ['y' => 2]]
);
// ['a' => ['x' => 1, 'y' => 2]]Returns true if all elements satisfy the predicate.
use function Recruiter\Array\array_all;
$allEven = array_all([2, 4, 6], fn($n) => $n % 2 === 0); // trueReturns true if at least one element satisfies the predicate.
use function Recruiter\Array\array_some;
$hasEven = array_some([1, 2, 3], fn($n) => $n % 2 === 0); // trueRecursively flattens nested arrays.
use function Recruiter\Array\array_flatten;
$flat = array_flatten([1, [2, [3, [4, 5]]]]); // [1, 2, 3, 4, 5]Extracts a column from an array of arrays.
use function Recruiter\Array\array_pluck;
$names = array_pluck([
['name' => 'Alice', 'age' => 30],
['name' => 'Bob', 'age' => 25]
], 'name');
// ['Alice', 'Bob']Groups array elements by the result of a function.
use function Recruiter\Array\array_group_by;
$grouped = array_group_by([1, 2, 3, 4, 5, 6], fn($n) => $n % 2);
// [1 => [1, 3, 5], 0 => [2, 4, 6]]Returns the Cartesian product of multiple arrays.
use function Recruiter\Array\array_cartesian_product;
$product = array_cartesian_product([[1, 2], ['a', 'b']]);
// [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]Safe array access with optional fallback.
use function Recruiter\Array\array_fetch;
$value = array_fetch(['foo' => 'bar'], 'foo'); // 'bar'
$value = array_fetch([], 'missing', 'default'); // 'default'
$value = array_fetch([], 'key', fn($k) => "no $k"); // 'no key'Access nested array values using a path of keys.
use function Recruiter\Array\array_get_in;
$data = ['user' => ['name' => ['first' => 'John']]];
$firstName = array_get_in($data, ['user', 'name', 'first']); // 'John'
$missing = array_get_in($data, ['user', 'age'], 0); // 0Updates an array element if it exists and is not null.
use function Recruiter\Array\array_update;
$updated = array_update(['count' => 5], 'count', fn($n) => $n + 1);
// ['count' => 6]Transforms a flat array with dot-notation keys into a nested structure.
use function Recruiter\Array\array_as_hierarchy;
$hierarchy = array_as_hierarchy([
'user.name' => 'John',
'user.email' => 'john@example.com',
'settings.theme' => 'dark'
]);
// ['user' => ['name' => 'John', 'email' => 'john@example.com'],
// 'settings' => ['theme' => 'dark']]Returns the maximum value in an array, or null if empty.
use function Recruiter\Array\array_max;
$max = array_max([3, 1, 4, 1, 5]); // 5Checks if array1 is a subset of array2.
use function Recruiter\Array\array_subset;
$isSubset = array_subset([1, 2], [1, 2, 3]); // trueChecks if an array has only numeric keys.
use function Recruiter\Array\is_numeric_array;
$numeric = is_numeric_array([1, 2, 3]); // true
$assoc = is_numeric_array(['a' => 1, 'b' => 2]); // falseThe package includes a Range class for creating numeric ranges:
use Recruiter\Array\Range;
$range = new Range(1, 5); // Iterates from 1 to 4
foreach ($range as $n) {
echo $n; // 1, 2, 3, 4
}
// Works with array functions
$doubled = array_map(new Range(1, 4), fn($n) => $n * 2); // [2, 4, 6]- PHP 8.4+
- Docker and Make for development environment
git clone https://github.com/recruiterphp/array.git
cd array
make build
make installmake test # Run PHPUnit tests
make phpstan # Run static analysis
make fix-cs # Fix code style
make rector # Run automated refactoringmake up # Start Docker containers
make down # Stop Docker containers
make shell # Open container shell
make clean # Clean up containers and volumesThis package replaces the legacy onebip/onebip-array package. To migrate:
-
Update your
composer.json:"require": { "recruiterphp/array": "^1.0" }
-
The namespace remains
Onebipfor backward compatibility -
All functions maintain the same signatures
Contributions are welcome! Please ensure:
- All tests pass (
make test) - Static analysis passes (
make phpstan) - Code style is fixed (
make fix-cs) - New features include tests
MIT License. See LICENSE for details.
RecruiterPHP is a suite of battle-tested PHP packages for building robust, scalable applications. The array package is part of our toolkit that includes job queues, event sourcing, concurrency control, and more.
Visit recruiterphp.org for more information about the full ecosystem.