webdevcave / collections
A PHP library for managing collections of data with support for nested keys.
dev-master
2024-05-14 20:15 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2024-12-14 21:32:01 UTC
README
The Collection class represents a collection of data with support for nested keys.
Installation
You can install the Collection class via Composer:
composer require webdevcave/collections
Usage
Instantiate the Collection class with an optional array of initial data:
use WebdevCave\Collections\Collection; $collection = new Collection([ 'user' => [ 'name' => 'John Doe', 'email' => 'john@example.com', 'address' => [ 'city' => 'New York', 'country' => 'USA' ] ] ]);
Access nested data using dot notation:
$city = $collection->get('user.address.city'); // Returns 'New York'
Check if a nested key exists:
$hasCountry = $collection->has('user.address.country'); // Returns true
Set a value for a nested key:
$collection->set('user.address.postal_code', '10001');
Delete a key and its value:
$collection->delete('user.address.city');
Clear the collection:
$collection->clear();
Contributing
Bug reports, suggestions and pull requests are welcome on GitHub.
License
The class is available as open source under the terms of the MIT License.