pragmarx/ia-arr
Illuminate\Support\Arr extracted from Laravel, repackaged as a framework-agnostic PHP library. Provides the full set of Arr helpers under the IlluminateAgnostic\Arr namespace to avoid conflicts, usable in any project (including Laravel).
Installation:
composer require pragmarx/ia-arr
Add the namespace to your project:
use IlluminateAgnostic\Arr\Support\Arr;
First Use Case:
Replace native PHP array operations with Arr helpers. For example:
$data = ['user' => ['name' => 'John', 'email' => 'john@example.com']];
$email = Arr::get($data, 'user.email'); // Returns 'john@example.com'
Key Documentation:
Arr helper docs for method signatures.Data Extraction:
Use Arr::get(), Arr::get() with defaults, or Arr::only()/Arr::except() to filter nested arrays:
$user = Arr::only($data, ['user.name', 'user.email']);
Modification:
Leverage Arr::set(), Arr::prepend(), or Arr::pull() for in-place updates:
Arr::set($data, 'user.address.city', 'New York');
Validation & Checks:
Combine with Arr::has() or Arr::exists() for conditional logic:
if (Arr::has($data, 'user.settings')) {
// Handle nested settings
}
Collection Integration:
Convert arrays to Illuminate\Support\Collection for fluent operations:
$collection = collect($data)->map(function ($value, $key) {
return Arr::get($value, 'name');
});
IlluminateAgnostic namespace to avoid conflicts with Laravel’s native Arr (e.g., in non-Laravel classes).symfony/var-dumper (included as a dependency) for debugging:
dump(Arr::get($data, 'user.*')); // Dump nested keys
Arr methods in unit tests:
$this->partialMock(Arr::class, ['get'])->willReturn('mocked_value');
Case Sensitivity:
Arr::get() is case-sensitive by default. Use Arr::get($array, 'user.Name') for exact matches or lowercase keys if needed.$normalized = array_change_key_case($array, CASE_LOWER);
Arr::get($normalized, 'user.name');
Deep Dot Notation:
null). Always provide defaults:
Arr::get($data, 'user.non_existent', []); // Returns empty array
PHP 7.0+ Only:
Collection vs. Array:
Arr::pluck() may behave differently on Collection vs. raw arrays. Prefer collect($array)->pluck() for consistency.dump(Arr::all($array)) to inspect nested structures.Arr::pull()). Store results in variables:
$value = Arr::pull($array, 'key');
$modified = Arr::set($array, 'new.key', $value);
Custom Helpers: Extend the package by creating a wrapper class:
class CustomArr extends Arr {
public static function customMethod(array $array) {
return self::get($array, 'custom.path', []);
}
}
Laravel Facade: In Laravel projects, create a facade to reuse the package’s methods:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Facade;
Facade::alias('Arr', IlluminateAgnostic\Arr\Support\Arr::class);
Performance:
For large arrays, cache results of expensive operations (e.g., Arr::pluck()) to avoid recomputation:
$cached = Arr::pluck($data, 'user.*');
cache()->put('user_data', $cached, now()->addHours(1));
Arr::set($array, $key, $value)).How can I help you explore Laravel packages today?