https://github.com/renoki-co/laravel-explicit-array
Improved Laravel dot notation for explicit array keys.
https://github.com/renoki-co/laravel-explicit-array
arr array laravel php support
Last synced: 12 months ago
JSON representation
Improved Laravel dot notation for explicit array keys.
- Host: GitHub
- URL: https://github.com/renoki-co/laravel-explicit-array
- Owner: renoki-co
- License: apache-2.0
- Created: 2021-10-27T19:28:18.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-27T04:27:40.000Z (about 2 years ago)
- Last Synced: 2025-03-17T23:57:27.845Z (about 1 year ago)
- Topics: arr, array, laravel, php, support
- Language: PHP
- Homepage:
- Size: 67.4 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Laravel Explicit Array
======================

[](https://codecov.io/gh/renoki-co/laravel-explicit-array/branch/master)
[](https://github.styleci.io/repos/421948177)
[](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[](https://packagist.org/packages/renoki-co/laravel-explicit-array)
Improved Laravel dot notation for explicit array keys.
## 🚀 Installation
You can install the package via composer:
```bash
composer require renoki-co/laravel-explicit-array
```
## 🙌 Usage
The original Laravel's `Arr::set()` method treats the dots within the key as separators for nested values. This is expected. The segments will create a nested value `some -> annotation -> com/ttl` with a value of `1800`.
```php
$annotations = [
'some.annotation.com/ttl' => 900,
];
Arr::set($annotations, 'some.annotation.com/ttl', 1800);
// Current result
// [
// 'some' => [
// 'annotation' => [
// 'com/ttl' => 1800
// ]
// ]
// ]
// Desired result
// [
// 'some.annotation.com/ttl' => 1800
// ]
```
To fix this, Explicit Array introduces a new `RenokiCo\ExplicitArray\Arr` class, which altered the `::set()` method, so that will make sure to read the segments between quotes as literal keys.
**You may use this class as your regular `Arr` class because it extends the original `\Illuminate\Support\Arr` class.**
```php
use RenokiCo\ExplicitArray\Arr;
Arr::set($annotations, '"some.annotation.com/ttl"', 1800);
// [
// 'some.annotation.com/ttl' => 1800
// ]
```
This can work with mixed segments, meaning that as long as you keep the dots outside the quotes, you can specify nested values:
```php
use RenokiCo\ExplicitArray\Arr;
Arr::set($annotations, 'annotations.nested."some.annotation.com/ttl"', 1800);
// [
// 'annotations' => [
// 'nested' => [
// 'some.annotation.com/ttl' => 1800
// ]
// ]
// ]
```
## 🐛 Testing
``` bash
vendor/bin/phpunit
```
## 🤝 Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## 🔒 Security
If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.
## 🎉 Credits
- [Alex Renoki](https://github.com/rennokki)
- [All Contributors](../../contributors)