-
Notifications
You must be signed in to change notification settings - Fork 0
4. API reference
This page provides a detailed reference for all the methods available in the flexible-collections package. Each method includes a description, usage examples, and the expected output.
- Creating a Collection
- Adding Elements
- Retrieving Elements
- Checking Elements
- Removing Elements
- Conditional Operations
To create a new collection instance, simply instantiate the Collection
class:
use Ulyssear\Collection;
$collection = new Collection();
Adds one or more elements to the collection.
$collection->push('PHP', 'Symfony', 'Laravel');
$collection = new Collection();
$collection->push('PHP', 'Symfony', 'Laravel');
print_r($collection->entries());
Array
(
[0] => PHP
[1] => Symfony
[2] => Laravel
)
Adds an element with a specific key.
$collection->pushNamedItem('language', 'PHP');
$collection = new Collection();
$collection->pushNamedItem('language', 'PHP');
print_r($collection->entries());
Array
(
[language] => PHP
)
Adds a named item only if it does not already exist.
$collection->pushNamedItemIfNotExists('framework', 'Laravel');
$collection = new Collection();
$collection->pushNamedItem('framework', 'Symfony');
$collection->pushNamedItemIfNotExists('framework', 'Laravel');
print_r($collection->entries());
Array
(
[framework] => Symfony
)
Adds multiple named elements to the collection.
$collection->pushNamedItems(['key1', 'value1'], ['key2', 'value2']);
Adds multiple named items only if they do not already exist.
$collection->pushNamedItemsIfNotExists(['key1', 'value1'], ['key2', 'value2']);
Adds elements in heap order.
$collection->pushHeap(5, 1, 3);
Adds elements in stack order (LIFO).
$collection->pushStack(2, 1);
Retrieves an element at a specific index.
$collection->get(1);
Returns all entries in the collection.
$collection->entries();
Returns all keys of the elements in the collection.
$collection->keys();
Returns all values of the elements in the collection.
$collection->values();
Checks if an element exists in the collection.
$collection->has('Symfony');
Checks if a specific key exists in the collection.
$collection->hasIndex('framework');
Removes and returns the last item added.
$collection->pop();
Removes and returns the first item added.
$collection->shift();
Clears the entire collection.
$collection->clear();
Adds one or more elements only if they do not already exist in the collection.
$collection->pushIfNotExists(3, 4);
This reference should help you understand the different methods and their usage in the flexible-collections package. For further examples, check out the Usage page.