Skip to content

3. Usage

Ulysse ARNAUD edited this page Sep 14, 2024 · 3 revisions

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.


Introduction

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.

Basic Example

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());

Output:

Array
(
    [0] => PHP
    [1] => Composer
    [2] => Ulyssear
)

Main Methods

1. push()

Adds one or more elements to the collection.

$collection = new Collection();
$collection->push('PHP', 'Symfony', 'Laravel');

print_r($collection->entries());

Output:

Array
(
    [0] => PHP
    [1] => Symfony
    [2] => Laravel
)

2. get($index)

Retrieves an element at a specific index.

echo $collection->get(1); // Displays 'Symfony'

3. pushNamedItem($key, $value)

Adds an element with a specific key.

$collection->pushNamedItem('language', 'PHP');
print_r($collection->entries());

Output:

Array
(
    [language] => PHP
)

4. pushNamedItemIfNotExists($key, $value)

Adds a named item only if it does not already exist.

$collection->pushNamedItem('framework', 'Symfony');
$collection->pushNamedItemIfNotExists('framework', 'Laravel');

print_r($collection->entries());

Output:

Array
(
    [language] => PHP
    [framework] => Symfony
)

5. has($value)

Checks if an item exists in the collection.

echo $collection->has('Symfony') ? 'Yes' : 'No'; // Displays 'Yes'

6. pop()

Removes and returns the last item added.

$last = $collection->pop();
echo $last; // Displays 'Symfony'

7. clear()

Completely clears the collection.

$collection->clear();
print_r($collection->entries()); // Displays an empty array

Advanced Usage

Conditional Adding with pushIfNotExists()

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());

Output:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

Using pushHeap() and pushStack()

  • 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());

Conclusion

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.