minwork/array
Minwork Array provides fast, well-tested helpers for nested, associative, and object arrays. Get/set/has plus map, filter, find, group, sort, and validation, with fluent-style chaining. No dependencies; modern PHP syntax.
Arr::map(array $array, callable $callback, int $mode = Arr::MAP_ARRAY_KEY_VALUE): array
Applies a callback to the elements of given array. Arguments supplied to callback differs depending on selected $mode.
{% hint style="warning" %}
For backward compatibility using map(callable, array) is still possible but is deprecated and will issue appropriate warning
{% endhint %}
| Constant name | Description |
|---|---|
| MAP_ARRAY_KEY_VALUE | Map array using callback in form of function($key, $value) |
| MAP_ARRAY_VALUE_KEYS_LIST | Map array using callback in form of function($value, $key1, $key2, ...) |
| MAP_ARRAY_KEYS_ARRAY_VALUE | Map array using callback in form of function(array $keys, $value) |
| MAP_ARRAY_VALUE_KEY | Map array using callback in form of function($value, $key) |
$array1 = ['a', 'b', 'c'];
$array2 = [
1 => [
2 => 'a',
3 => 'b',
4 => [
5 => 'c',
],
],
'test' => 'd',
];
$mapKeyValue = function ($key, $value) {
return "{$key} -> {$value}";
};
// Mind that $value is a first argument here
$mapValueKey = function ($value, $key) {
return "{$key} -> {$value}";
};
$mapKeysValue = function ($keys, $value) {
return implode('.', $keys) . " -> {$value}";
};
$mapValueKeysList = function ($value, $key1, $key2) {
return "$key1.$key2 -> {$value}";
};
// Equivalent to using MAP_ARRAY_KEY_VALUE as mode (3rd) argument
Arr::map($array1, $mapKeyValue) -> ['0 -> a', '1 -> b', '2 -> c']
// Resemble array_map function but with array supplied as first argument
Arr::map($array1, $mapValueKey) -> ['0 -> a', '1 -> b', '2 -> c']
// Map multidimensional array using keys array
Arr::map($array2, $mapKeysValue, Arr::MAP_ARRAY_KEYS_ARRAY_VALUE) ->
[
1 => [
2 => '1.2 -> a',
3 => '1.3 -> b',
4 => [
5 => '1.4.5 -> c',
],
],
'test' => 'test -> d',
]
// Map multidimensional array using keys list (mind that all keys above 2nd are ignored due to callback function syntax)
Arr::map($array2, $mapValueKeysList, Arr::MAP_ARRAY_VALUE_KEYS_LIST) ->
[
1 => [
2 => '1.2 -> a',
3 => '1.3 -> b',
4 => [
5 => '1.4 -> c',
],
],
'test' => 'test -> d',
]
Arr::mapObjects(array $objects, string $method, ...$args): array
Map array of object to values returned from objects method
$object = new class() {
function test($arg = 0) {
return 1 + $arg;
}
};
$array = [$object, $object, $object];
Arr::mapObjects($array, 'test') -> [1, 1, 1]
Arr::mapObjects($array, 'test', 2) -> [3, 3, 3]
How can I help you explore Laravel packages today?