The Collection class represents a collection of data with support for nested keys.
You can install the Collection class via Composer:
composer require webdevcave/collections
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();
Bug reports, suggestions and pull requests are welcome on GitHub.
The class is available as open source under the terms of the MIT License.