Collection Component


    is an object oriented array. It offers speed, as well as implementations of various PHP interfaces. These are:

    Constructor

    You can construct the object as any other object in PHP. However, the constructor accepts an optional array parameter, which will populate the object for you.

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);

    Reusing

    You can also reuse the component, by repopulating it. Phalcon\Collection exposes the clear() and init() methods, which will clear and repopulate the internal array respectively,

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. echo $collection->count(); // 2
    13. $data = [
    14. 'year' => 1776,
    15. ];
    16. $collection->clear();
    17. $collection->init($data);
    18. echo $collection->count(); // 1

    Get

    As mentioned above, Phalcon\Collection implements several interfaces, in order to make the component as flexible as possible. Retrieving data stored in an element can be done by using:

    • Property
    • __get()
    • array based get ($collection[$element])
    • offsetGet()
    • get()The fastest way is by using the property syntax:
    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. echo $collection->year; // 1776

    You can use __get($element) but it is not advisable as it is much slower than the property syntax. The same applies to offsetGet

    1. echo $collection->__get('year'); // 1776
    2. echo $collection['year']; // 1776
    3. echo $collection->offsetGet('year'); // 1776
    4. echo $collection->get('year', 1776, true); // 1776
    1. public function get(string $element, mixed $defaultValue = null, bool $insensitive = true): mixed

    Using get() offers two extra parameters. When $defaultValue is defined in the call, if the element is not found, $defaultValue will be returned. By default $insensitive is set to true, making searches in the collection case insensitive. Setting this value to false will make the search for the element case sensitive.

    • isset() on the property
    • __isset()
    • array based isset (isset($coollection[$element]))
    • offsetExists()
    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. echo isset($collection->year); // true

    You can use __isset(element) but it is not advisable as it is much slower than the property syntax. The same applies to offsetExists

    1. public function has(string $element, bool $insensitive = true): bool

    Using has() offers an extra parameter. By default $insensitive is set to true, making searches in the collection case insensitive. Setting this value to false will make the search for the element case sensitive.

    1. echo $collection->has('YEAR', true); // true
    2. echo $collection->has('YEAR', false); // false

    Set

    To set an element in the collection, you can use the following:

    • assign the value to the property
    • __set()
    • array based assignment
    • offsetSet()
    • set()The fastest way is by using the property syntax:
    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. ];
    10. $collection = new Collection($data);
    11. $collection->year = 1776;

    You can use __set($element, $value) but it is not advisable as it is much slower than the property syntax. The same applies to offsetSet

    1. $collection->__set('year', 1776);
    2. $collection['year'] = 1776;
    3. $collection->offsetSet('year', 1776);
    4. $collection->set('year', 1776);

    Remove

    To remove an element in the collection, you can use the following:

    • unset the property
    • __unset()
    • array based unset
    • offsetUnset()
    • remove()The fastest way is by using the property syntax:
    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. ];
    10. $collection = new Collection($data);
    11. unset($collection->year);

    You can use __unset($element) but it is not advisable as it is much slower than the property syntax. The same applies to offsetUnset

    1. $collection->__unset('year');
    2. unset($collection['year']);
    3. $collection->offsetUnset('year');
    4. $collection->remove('year');
    1. $collection->remove('YEAR', true);
    2. $collection->remove('YEAR', false);

    Iteration

    Since the collection object implements \IteratorAggregate, you can iterate through the object with ease. The method getIterator() returns an ArrayIterator() object

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. foreach ($collection as $key => $value) {
    13. echo $key . ' - ' . $value . PHP_EOL;

    The implementation of the \Countable interface exposes the count() method, which stores the number of elements in the collection.

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'blue',
    7. ],
    8. 'year' => 1776,
    9. ];
    10. $collection = new Collection($data);
    11. echo $collection->count(); // 2

    Serialization

    The \Serializable and \JsonSerializable interfaces expose methods that allow you to serialize and unserialize the object. serialize() and unserialize() use PHP’s serialize and unserialize functions. jsonSerialize() returns an array which can be used with json_encode to serialize the object.

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. echo $collection->serialize(); // a:2:{s:6:"colors";a:3:{i:0;s:3:"red";i:1;s:5:"white";i:2;s:4:"blue";}s:4:"year";i:1776;}
    13. $serialized = 'a:2:{s:6:"colors";a:3:{i:0;s:3:"red";i:1;s:5:"white";i:2;s:4:"blue";}s:4:"year";i:1776;}';
    14. $collection->unserialize($serialized);
    15. echo $collection->jsonSerialize(); // $data

    Transformations

    Phalcon\Collection also exposes two transformation methods: toArray() and toJson(int $options). toArray() returns the object transformed as an array. This method returns the same array as jsonSerialize().

    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. echo $collection->toArray(); // $data

    toJson(int $options) returns a JSON representation of the object. It uses json_encode internally and accepts a parameter, which represents the flags that json_encode accepts. By default the options are set up with the value 74, () which translates to:

    • JSON_HEX_TAG
    • JSON_HEX_APOS
    • JSON_HEX_AMP
    • JSON_HEX_QUOT
    • JSON_UNESCAPED_SLASHESYou can pass any valid flags to the method according to your needs.
    1. <?php
    2. use Phalcon\Collection;
    3. $data = [
    4. 'colors' => [
    5. 'red',
    6. 'white',
    7. 'blue',
    8. ],
    9. 'year' => 1776,
    10. ];
    11. $collection = new Collection($data);
    12. echo $collection->toJson(); // ["red","white","blue"],"year":1776}
    13. echo $collection->toJson(74 + JSON_PRETTY_PRINT);
    14. /**
    15. {
    16. "colors": [
    17. "red",
    18. "white",
    19. "blue"
    20. ],
    21. "year": 1776
    22. }
    23. */

    Read Only

    Phalcon also offers a component that can be used in a read-only fashion. Phalcon\Collection\ReadOnly can serve as a collection in your application that can only be populated with initial data but not allowing its contents to be changed throughout the application.