yiisoft/arrays
yiisoft/arrays is a lightweight PHP utility library for working with arrays: fetch values by key/path, set and remove nested items, merge and filter data, and perform common transformations. Designed for Yii projects but usable standalone.
Installation Add the package via Composer:
composer require yiisoft/arrays
No additional configuration is required—it’s a standalone helper.
First Use Case Import the helper and use it for basic array operations:
use Yiisoft\Arrays\ArrayHelper;
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$value = ArrayHelper::getValue($array, 'b'); // Returns 2
Key Methods to Explore
getValue(): Retrieve nested array values (e.g., getValue($array, 'user.name')).setValue(): Set nested array values.merge(): Deep merge arrays.filter(): Filter arrays by callback.map(): Transform arrays with a callback.Nested Data Handling
Use getValue()/setValue() for Eloquent models or API responses:
$user = ['user' => ['name' => 'John', 'address' => ['city' => 'NY']]];
$city = ArrayHelper::getValue($user, 'user.address.city'); // 'NY'
Form Request Validation Flatten or sanitize request data:
$requestData = ArrayHelper::toArray($request->all());
$filtered = ArrayHelper::filter($requestData, fn($val) => !empty($val));
Configuration Merging Deep merge configs (e.g., environment-specific overrides):
$baseConfig = require 'config/base.php';
$envConfig = require 'config/env.php';
$merged = ArrayHelper::merge($baseConfig, $envConfig);
Collection-Like Operations
Replace collect() for simple array transformations:
$prices = [100, 200, 300];
$discounted = ArrayHelper::map($prices, fn($price) => $price * 0.9);
Laravel Service Providers Bind the helper as a singleton for global access:
$this->app->singleton('arrayHelper', fn() => new ArrayHelper());
Then inject via constructor or resolve with app('arrayHelper').
Form Requests
Use ArrayHelper::getValue($request->all(), 'user.id') to safely access nested form fields.
API Responses Normalize responses before JSON encoding:
$response = ArrayHelper::toArray($model->toArray());
Path Syntax in getValue/setValue
user.address.city), but invalid paths return null (not [] or exceptions).getValue(['a' => 1], 'a.b') → null.Deep Merge Overwrites
ArrayHelper::merge() recursively merges, but later keys overwrite earlier ones for duplicate paths.merge(['a' => 1], ['a' => 2]) // ['a' => 2]
Callback Context in filter/map
// ❌ Avoid (modifies original array)
ArrayHelper::map($array, fn(&$val) => $val++);
Validate Paths
Use ArrayHelper::getValue($array, 'path', null) to test paths before relying on them.
Inspect Merged Arrays For complex merges, log intermediate steps:
$step1 = ArrayHelper::merge($a, $b);
$step2 = ArrayHelper::merge($step1, $c);
Custom Callbacks
Extend filter()/map() with reusable callbacks:
$isActive = fn($val) => $val === 'active';
ArrayHelper::filter($statuses, $isActive);
Path Manipulation
Combine with Str::of() for dynamic paths:
use Yiisoft\Arrays\ArrayHelper;
use Illuminate\Support\Str;
$path = Str::of('user')->append('.address')->toString();
ArrayHelper::getValue($data, $path);
Integration with Laravel Collections
Use ArrayHelper for preprocessing before converting to collections:
$cleaned = ArrayHelper::filter($rawData, fn($v) => $v !== null);
collect($cleaned)->pluck('id');
Avoid Overuse in Loops
getValue()/setValue() are not optimized for bulk operations—use native PHP or array_walk for loops.
Memory Efficiency
For large arrays, prefer ArrayHelper::filter() over array_filter() with closures if the helper’s implementation is more memory-efficient (benchmark first).
How can I help you explore Laravel packages today?