Download the PHP package dpolac/dictionary without Composer
On this page you can find all versions of the php package dpolac/dictionary. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dpolac/dictionary
More information about dpolac/dictionary
Files in dpolac/dictionary
Package dictionary
Short Description Collection indexed with objects and scalars
License MIT
Informations about the package dictionary
PHP Dictionary
Collection indexed with objects and scalars
Example
Installation
Install via Composer:
Usage
Class \DPolac\Dictionary
implements Iterator
, ArrayAccess
,
Countable
and Serializable
. It also provides methods for
creating and sorting Dictionary and for converting it to array.
Valid types of keys for Dictionary are:
- object
- integer
- float
- string
- bool
- null
You cannot use:
- Closure
- array
Creating
To create empty Dictionary, use constructor.
You can also create Dictionary from key-value pairs.
Last option is to create Dictionary from array.
Converting to PHP array
There are three methods that let you retrieve data as array:
Dictionary::keys()
- returns array of keys,Dictionary::values()
- returns array of values,Dictionary::toPairs()
- returns array of key-value pairs; each pair is 2-element array.
Copying Dictionary
Unlike an array, Dictionary is an object and that means it
is reference type. If you want the copy of Dictionary, you have
to use clone
keyword or call Dictionary::getCopy()
method.
Sorting elements
Just like an array, Dictionary is ordered. To sort Dictionary, use
Dictionary::sortBy($callback, $direction)
method. Any argument
can be omitted.
$callback
will be called for every element. Dictionary will be ordered by values returned by callback. First argument of the callback is value and second is key of element. Instead of callable, you can use"values"
or"keys"
string.$direction
can be"asc"
or"desc"
. Default value is"asc"
.
Examples of sorting:
sortBy
changes Dictionary it is called for. If you want sorted copy,
chain it with getCopy
.
All versions of dictionary with dependencies
ext-spl Version *