---
title: "Dictionaries"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries
language: php
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Dictionaries

Programming language: PHP. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/matlab.md), [Python](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/dictionaries/swift.md).

## Dictionary Syntax and Semantics

A dictionary is an associative array: a collection of key-value pairs in which each key is unique.

For example:

```slice
module M
{
    struct Employee
    {
        long   number;
        string firstName;
        string lastName;
    }

    dictionary<long, Employee> EmployeeMap;
}
```

This definition creates a dictionary named `EmployeeMap` that maps from an employee number to a structure containing the details for an employee.

## Allowable Types for Dictionary Keys and Values

The value type of a dictionary can be any Slice type. However, the key type of a dictionary is limited to one of the following types:

- [Integral](https://docs.zeroc.com/ice/3.8/slice/basic-types/php.md) types (`short`, `int`, `long`)
- [bool](https://docs.zeroc.com/ice/3.8/slice/basic-types/php.md)
- [byte](https://docs.zeroc.com/ice/3.8/slice/basic-types/php.md)
- [string](https://docs.zeroc.com/ice/3.8/slice/basic-types/php.md)
- [enum](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/enumerations/php.md)
- [Structures](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/php.md) containing only fields of legal key types

## Language Mapping

A Slice dictionary maps to a native PHP associative array: a dictionary whose key type is an enumeration or one of `bool`, `byte`, `short`, `int`, or `long` maps to an array with integer keys, and a dictionary with a `string` key type maps to an array with string keys.

Ice for PHP does not support dictionaries whose key type is a structure.

> **Note**
>
> Boolean values are treated as integers, with false equivalent to 0 (zero) and true equivalent to 1 (one).

Here is the definition of our EmployeeMap:

```slice
dictionary<long, Employee> EmployeeMap;
```

You can create an instance of this dictionary as shown below:

```php
$e1 = new Employee;
$e1->number = 42;
$e1->firstName = "Stan";
$e1->lastName = "Lippman";

$e2 = new Employee;
$e2->number = 77;
$e2->firstName = "Herb";
$e2->lastName = "Sutter";

$em = [$e1->number => $e1, $e2->number => $e2];
```
