-
Notifications
You must be signed in to change notification settings - Fork 0
3. Usage
Welcome to the usage page for the flexible-collections package. This page details the different methods available and provides practical examples to help you get the most out of this package.
The flexible-collection package allows you to create and manipulate data collections flexibly and efficiently in PHP. Whether you need to manage a list of items or handle data with specific keys, this package offers a rich and intuitive API.
To get started, here's a simple example showing how to create a collection and add elements:
use Ulyssear\Collection;
$collection = new Collection();
$collection->push('PHP', 'Composer', 'Ulyssear');
// Display the added elements
print_r($collection->entries());
Array
(
[0] => PHP
[1] => Composer
[2] => Ulyssear
)
Adds one or more elements to the collection.
$collection = new Collection();
$collection->push('PHP', 'Symfony', 'Laravel');
print_r($collection->entries());
Array
(
[0] => PHP
[1] => Symfony
[2] => Laravel
)
Retrieves an element at a specific index.
echo $collection->get(1); // Displays 'Symfony'
Adds an element with a specific key.
$collection->pushNamedItem('language', 'PHP');
print_r($collection->entries());
Array
(
[language] => PHP
)
Adds a named item only if it does not already exist.
$collection->pushNamedItem('framework', 'Symfony');
$collection->pushNamedItemIfNotExists('framework', 'Laravel');
print_r($collection->entries());
Array
(
[language] => PHP
[framework] => Symfony
)
Checks if an item exists in the collection.
echo $collection->has('Symfony') ? 'Yes' : 'No'; // Displays 'Yes'
Removes and returns the last item added.
$last = $collection->pop();
echo $last; // Displays 'Symfony'
Completely clears the collection.
$collection->clear();
print_r($collection->entries()); // Displays an empty array
Adds one or more items to the collection only if they are not already present.
$collection->push(1, 2, 3);
$collection->pushIfNotExists(3, 4);
print_r($collection->entries());
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
-
pushHeap()
: Adds elements in heap order.
$collection->pushHeap(5, 1, 3);
print_r($collection->entries());
-
pushStack()
: Adds elements in stack order (LIFO).
$collection->pushStack(2, 1);
print_r($collection->entries());
These are just a few examples of what you can do with the flexible-collections package. Explore the various methods and discover how they can help you manage your data collections more effectively.
For a complete description of all available methods, please refer to the API Reference section.