Installation Add via Composer:
composer require voku/arrayy
No Laravel-specific setup is neededâjust autoload the package.
Basic Usage Import the core class:
use voku\arrayy\Arrayy;
Initialize with an array:
$array = Arrayy::from([1, 2, 3, 'a', 'b']);
First Use Case: Filtering Remove empty values:
$filtered = $array->filterEmpty();
// Result: [1, 2, 3, 'a', 'b']
Data Transformation
Use map() for value transformations:
$doubled = $array->map(fn($val) => $val * 2);
// Result: [2, 4, 6, 'aa', 'bb']
Nested Array Handling Flatten multi-dimensional arrays:
$flat = Arrayy::from([1, [2, 3]])->flatten();
// Result: [1, 2, 3]
Or extract values from nested keys:
$users = Arrayy::from([['name' => 'Alice'], ['name' => 'Bob']])->pluck('name');
// Result: ['Alice', 'Bob']
Conditional Logic Filter with custom callbacks:
$evens = $array->filter(fn($val) => $val % 2 === 0);
// Result: [2]
Laravel Integration Use with Eloquent collections:
$collection = User::all()->toArray();
$processed = Arrayy::from($collection)->filterEmpty();
Batch Processing Chunk arrays for large datasets:
$chunks = $array->chunk(2);
// Result: [[1, 2], [3, 'a'], ['b']]
Immutable Operations
Methods like map() return new arraysâthey donât modify the original. Chain methods carefully:
// â Overwrites $array
$array->map(...)->filter(...);
// â
Safe
$result = $array->map(...)->filter(...);
Key Preservation
Some methods (e.g., filter()) may reindex arrays. Use filter() with true to preserve keys:
$array->filter(fn($val) => $val > 1, true);
Type Sensitivity
arrayy treats null, false, 0, and '' as "empty" in filterEmpty(). Explicitly handle edge cases if needed.
Performance
Avoid deep recursion on large arrays (e.g., flatten()). For nested structures, consider iterative approaches.
Arrayy::from($array)->toArray() to debug transformations.dd()
Combine with Laravelâs helpers:
dd(Arrayy::from($data)->pluck('key')->toArray());
Custom Methods
Extend Arrayy via traits or subclasses:
use voku\arrayy\Arrayy as BaseArrayy;
class CustomArrayy extends BaseArrayy {
public function customMethod() { ... }
}
Integration with Laravel Collectors Convert to/from Laravel collections:
$laravelCollection = collect(Arrayy::from($array)->toArray());
Configuration
No package-wide config exists, but you can wrap Arrayy in a service provider for global defaults (e.g., strict typing).
How can I help you explore Laravel packages today?