flow-php/array-dot
Flow PHP Array Dot adds easy dot-notation access to PHP arrays. Read, set, and manipulate deeply nested values with a clean API—ideal for config data, decoded JSON, and complex structures—making array handling clearer and less error-prone.
Installation:
composer require flow-php/array-dot
No Laravel-specific setup required—works as a standalone package.
First Use Case: Replace verbose nested array access with dot notation:
use Flow\ArrayDot\ArrayDot;
$array = [
'user' => [
'name' => 'John',
'address' => [
'city' => 'New York'
]
]
];
$dot = new ArrayDot($array);
echo $dot->get('user.address.city'); // Output: "New York"
Where to Look First:
Dot Notation Access:
$value = $dot->get('path.to.value', 'default');
$dot->set('path.to.value', 'new_value');
if ($dot->has('path.to.value')) { ... }
Laravel Integration:
$this->app->singleton('array-dot', fn() => new ArrayDot());
ArrayDotFacade.php):
use Illuminate\Support\Facades\Facade;
class ArrayDotFacade extends Facade { protected static function getFacadeAccessor() { return 'array-dot'; } }
Usage:
ArrayDot::get('request.user.id');
ETL Workflows:
$data = array_dot::transform($array, fn($value, $path) => strtoupper($value));
$flat = array_dot::flatten($nestedArray);
$nested = array_dot::unflatten($flatArray);
Request/Response Handling:
public function handle($request, Closure $next) {
$request->merge(array_dot::dot($request->all()));
return $next($request);
}
return response()->json(array_dot::dot($eloquentData));
Validation:
$rules = [
'user.address.city' => 'required|string',
'user.profile.*' => 'nullable'
];
Data Migration:
$migrated = array_dot::dot($legacyArray, ['user', 'address']);
Configuration Management:
$defaultConfig = require 'config/default.php';
$envConfig = require 'config/env.php';
$merged = array_dot::merge($defaultConfig, $envConfig);
Testing:
$this->assertEquals('New York', array_dot::get($data, 'user.address.city'));
Combine with Laravel’s Arr:
array_dot for complex operations, Arr::dot() for simple cases:
use Illuminate\Support\Arr;
Arr::set($array, 'user.address.city', 'Boston'); // Simple
array_dot::set($array, 'user.address.city', 'Boston'); // Complex paths
Performance Optimization:
$cache = [];
foreach ($largeArray as $item) {
$cache[$item['id']] = array_dot::get($item, 'user.profile.name');
}
Immutable Operations:
array_dot::dot() + array_merge for immutability:
$newArray = array_merge($original, array_dot::dot($original, ['path', 'to', 'update']));
Overwriting vs. Merging:
set() overwrites by default. Use merge() for partial updates:
// Overwrites entire 'address' key
$dot->set('user.address', ['city' => 'Boston']);
// Merges with existing 'address'
$dot->merge('user.address', ['city' => 'Boston']);
Circular References:
if (array_dot::hasCircularReferences($array)) {
throw new \RuntimeException('Circular reference detected');
}
Non-Array Inputs:
if (!is_array($data)) {
$data = [];
}
PHP 8.3+ Requirement:
if (version_compare(PHP_VERSION, '8.3.0') < 0) {
throw new \RuntimeException('PHP 8.3+ required');
}
Performance with Deep Arrays:
$start = microtime(true);
array_dot::get($deepArray, 'a.b.c.d.e.f');
$time = microtime(true) - $start;
Invalid Dot Paths:
null silently. Add logging:
$value = array_dot::get($array, 'invalid.path');
if ($value === null) {
\Log::warning("Dot path 'invalid.path' not found");
}
Unexpected Mutations:
set()/merge() modifies the original array. Use dot() for immutable copies:
$immutable = array_dot::dot($originalArray);
array_dot::set($immutable, 'path', 'value');
Case Sensitivity:
'User.Name' ≠ 'user.name'). Normalize keys if needed:
$normalized = array_dot::transform($array, fn($v, $k) => strtolower($k), true);
Laravel-Specific Extensions:
trait DotAccessible {
public function getDotAttribute($key) {
return array_dot::get($this->attributes, $key);
}
}
Wildcards:
* for partial matches (e.g., 'user.*'). Combine with array_dot::filter():
$filtered = array_dot::filter($array, fn($path) => str_contains($path, 'user.'));
Configuration:
$this->app->when(ArrayDot::class)
->needs('$strict')
->give(true); // Enable strict mode
Testing:
ArrayDot in PHPUnit:
$mock = $this->createMock(ArrayDot::class);
$mock->method('get')->willReturn('mocked_value');
Alternatives:
Illuminate\Support\Arr::dot() for simpler cases:
Arr::set($array, 'user.address.city', 'Boston'); // Built-in
array_dot::set($array, 'user.address.city', 'Boston'); // Advanced
How can I help you explore Laravel packages today?