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.
set method now also allow to supply object implementing ArrayAccess instead of pure array as first argument, as well as handling nested ArrayAccess objects.
Added new mode to check method that enables checking if only one of the array elements meet specified condition.
Also greatly improved check documentation - https://minwork.gitbook.io/array/validating-array/check
Added Arr::filter method which is a wrapper around array_filter to allow chaining in ArrObj
Added method to traverse through array or iterable object without modifying it. More info in documentation: https://minwork.gitbook.io/array/traversing-array/iterating
Added method to find element(s) inside an array or iterable object. More info in documentation: https://minwork.gitbook.io/array/manipulating-array/finding
ArrObj which can be used to easily chain Arr methods.ArrObj instance can be obtained by either explicitly calling it's constructor with new ArrObj(...) or for even easier chaining Arr::obj(...) method.
// Chain setting nested array values
Arr::obj()->set('foo', 'bar')->set('test.[]', 'test')->getArray() ->
[
'foo' => 'bar',
'test' => ['test']
]
// Quickly flatten array of objects grouped by id
Arr::obj([...])->groupObjects('getId')->flattenSingle()->getArray()
What's more, ArrObj contain PHPDoc for every method that can be used in it, so you can take advantage of your editor autocomplete feature.
Full documentation of this feature, coming soon.
Added new Arr::map mode (MAP_ARRAY_VALUE_KEY) to resemble native array_map but with array as first argument and callback as second.
Also added 4 new utility methods for getting first and last key or value from array:
getFirstKeygetLastKeygetFirstValuegetLastValue$keysNew method
Check if (nested) element with specified keys exists in array
New method
Remove (nested) element from array at path specified by keys
Added alias set
Added alias get
Inverse of unpack method.
Updated documentation regarding new modes.
$arr['foo.bar.0'] = 'a';
$arr['foo.bar.1'] = 'b';
$arr['foo.bar'] = ['a', 'b'];
$arr['foo.bar'] = ['a' => 1, 'b' => 2];
testUnpack$strict flag worksCheck if any element of an array is also an array.
For example [1, 2, 3] is a regular array so it isn't nested and [1, 2 => [], 3] has another array as one of it's elements so it is nested.
Get nesting depth of an array.
Taking example from above, [1, 2, 3] is a regular array so it has depth of 1 and [1, 2 => [], 3] has depth of 2 because of a second array in it. More detailed examples showing more complex cases can be found in documentation.
Calculate intersection between two or more arrays of objects using objects hashes for comparison.
New method sortObjects can be used for sorting array of objects by supplying method name (possibly with method arguments) to call on every object in array
For use cases check out updated examples section
How can I help you explore Laravel packages today?