When instantiating the object you can specify a second bool parameter, which will control the key searching in the object. 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 in a case-sensitive manner.
Using get() offers three parameters. $key is the key of the element we want to retrieve
If $defaultValue is set, it will be returned if the $key is not set or the $key is set and its value is null
The cast parameter accepts a string that defines what the returned value will be cast. The available values are:
The collection object also offers two more getters getKeys and getValuesgetKeys( bool $insensitive = true ) returns all the keys stored internally in the collection. By default, it will return the keys case-insensitive manner i.e. all lowercase. If false is passed in the call, it will return the keys exactly as they have been stored. getValues returns the values stored in the internal collection.
To check whether an element exists or not in the collection, you can use the following:
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.
Phalcon\Support\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().
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 79, (RFC4327) which translates to:
You can pass any valid flags to the method according to your needs.
Phalcon also offers a component that can be used in a read-only fashion. Phalcon\Support\Collection\ReadOnlyCollection 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.
This class has been renamed from ReadOnly in order to avoid collisions with PHP 8.x reserved words.