sajadsdi/array-dot-notation
Lightweight, high-performance PHP helper to get, set, delete, and check nested array values using dot notation. Supports multi-key operations, default values, key mapping on output, and callbacks for get/set behavior.
Installation:
composer require sajadsdi/array-dot-notation
No additional configuration is required—just autoload via Composer.
First Use Case: Convert a flat array to dot notation for nested access:
use Sajadsdi\ArrayDotNotation\ArrayDotNotation;
$array = ['user' => ['name' => 'John', 'age' => 30]];
$dotArray = ArrayDotNotation::dot($array);
// Access nested values with dot notation
echo $dotArray['user.name']; // Output: "John"
Where to Look First:
dot() and undot() methods on sample arrays to understand transformations.Dot Notation Conversion:
$config = ['database' => ['host' => 'localhost']];
$dotConfig = ArrayDotNotation::dot($config);
// $dotConfig['database.host'] = 'localhost'
$dotData = ['user.address.city' => 'New York'];
$nestedData = ArrayDotNotation::undot($dotData);
// $nestedData['user']['address']['city'] = 'New York'
Dynamic Access:
$settings = ArrayDotNotation::dot($_POST);
if ($settings['user.settings.notifications.enabled']) {
// Handle enabled notifications
}
Integration with Laravel:
Request->all() to dot notation for cleaner access:
$input = ArrayDotNotation::dot(request()->all());
$theme = $input['app.theme']; // Instead of $input['app']['theme']
$config = ArrayDotNotation::undot(config('custom.dot_notation'));
Validation:
$rules = [
'user.name' => 'required|string',
'user.address.city' => 'string|max:50',
];
$dotInput = ArrayDotNotation::dot(request()->input());
$validator = Validator::make($dotInput, $rules);
Overwriting Keys:
$array = ['a.b' => 1, 'a' => ['b' => 2]];
ArrayDotNotation::dot($array); // 'a.b' becomes 2 (overwritten)
ArrayDotNotation::dot($array, false) to preserve original keys.Non-String Keys:
$array = [1 => ['name' => 'Test']];
ArrayDotNotation::dot($array); // Throws error or behaves unexpectedly
Performance:
undot() can be slow for large arrays due to recursive rebuilding. Test with production-sized data.Laravel Caching:
config()) may not update if the underlying array changes. Re-convert on demand if needed.Verify Transformations:
print_r() or dd() to inspect transformed arrays:
$dotArray = ArrayDotNotation::dot($array);
dd($dotArray); // Check for unexpected keys/values
Edge Cases:
null values.Custom Separators:
.. Override it globally:
ArrayDotNotation::setSeparator('__');
$array = ['user__name' => 'John']; // Now uses `__` instead of `.`
Custom Dot Paths:
// Example: Get all keys under a path
function getDotKeys(array $dotArray, string $path): array {
$keys = [];
foreach ($dotArray as $key => $value) {
if (str_starts_with($key, $path . '.')) {
$keys[] = str_replace($path . '.', '', $key);
}
}
return $keys;
}
Integration with Laravel Collections:
$collection = collect($array)->mapWithKeys(function ($item, $key) {
return ArrayDotNotation::dot($item, false);
});
Middleware for Dot Notation:
public function handle($request, Closure $next) {
$request->merge(ArrayDotNotation::dot($request->all()));
return $next($request);
}
Now access $request->input('user.name') directly.How can I help you explore Laravel packages today?