Skip to content

WebdevCave/collections-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Collection Class

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.

Releases

No releases published

Packages

No packages published

Languages