2017 © Pedro Peláez
 

library dictionary

Collection indexed with objects and scalars

image

dpolac/dictionary

Collection indexed with objects and scalars

  • Sunday, June 5, 2016
  • by dpolac
  • Repository
  • 1 Watchers
  • 1 Stars
  • 36,157 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 14 % Grown

The README.md

PHP Dictionary

Collection indexed with objects and scalars, (*1)


Example

<?php
    $dict = new \DPolac\Dictionary();

    $dict[new \stdClass()] = 12;
    $dict['php'] = 23;
    $dict[100] = 'dictionary';

, (*2)

Installation

Install via Composer:

composer require dpolac/dictionary

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., (*3)

Valid types of keys for Dictionary are: - object - integer - float - string - bool - null, (*4)

You cannot use: - Closure - array, (*5)

Creating

To create empty Dictionary, use constructor., (*6)

<?php
    $dict = new \DPolac\Dictionary();

You can also create Dictionary from key-value pairs., (*7)

<?php
    $dict = \DPolac\Dictionary::fromPairs([
        ['key1', 'value1'],
        ['key2', 'value2'],
        ['key3', 'value3'],
    ]);

Last option is to create Dictionary from array., (*8)

<?php
    $dict = \DPolac\Dictionary::fromArray([
        'key1' => 'value1',
        'key2' => 'value2',
        'key3' => 'value3',
    ]);

Converting to PHP array

There are three methods that let you retrieve data as array:, (*9)

  • 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., (*10)

Sorting elements

Just like an array, Dictionary is ordered. To sort Dictionary, use Dictionary::sortBy($callback, $direction) method. Any argument can be omitted., (*11)

  • $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:, (*12)

<?php
    $dictionary->sortBy('values','asc');
<?php
    $dictionary->sortBy(function($value, $key) {
        return $value->title . $key->name;
    }, 'desc');

sortBy changes Dictionary it is called for. If you want sorted copy, chain it with getCopy., (*13)

<?php
    $sortedDictionary = $dictionary->getCopy()->sortBy('values', 'asc');

The Versions

05/06 2016

dev-master

9999999-dev

Collection indexed with objects and scalars

  Sources   Download

MIT

The Requires

  • php ^5.4 || ^7.0
  • ext-spl *

 

by Damian Polac

array hash dictionary splobjectstorage

05/06 2016

v1.0.0

1.0.0.0

Collection indexed with objects and scalars

  Sources   Download

MIT

The Requires

  • php ^5.4 || ^7.0
  • ext-spl *

 

by Damian Polac

array hash dictionary splobjectstorage