# Arrayable

> Arrayable is the interface that should be implemented by classes who want to support customizable representation of their instances.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [base](/docs/api/yii/base/index.md) › Arrayable

| interface | `yii\base\Arrayable` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/base/Arrayable.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/base/Arrayable.php#L24) |

Implemented by 1 type:

- [`Model`](/docs/api/yii/base/model/index.md)

Arrayable is the interface that should be implemented by classes who want to support customizable representation of their instances.

For example, if a class implements Arrayable, by calling [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray), an instance of this class
can be turned into an array (including all its embedded objects) which can then be further transformed easily
into other formats, such as JSON, XML.

The methods [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields) and [`extraFields()`](/docs/api/yii/base/arrayable/index.md#method-extraFields) allow the implementing classes to customize how and which of their data
should be formatted and put into the result of [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray).

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`extraFields()`](#method-extraFields) abstract | Returns the list of additional fields that can be returned by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray) in addition to those listed in [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields). | `Arrayable` |
| [`fields()`](#method-fields) abstract | Returns the list of fields that should be returned by default by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray) when no specific fields are specified. | `Arrayable` |
| [`toArray()`](#method-toArray) abstract | Converts the object into an array. | `Arrayable` |

## Method details

### extraFields() <span id="method-extraFields"></span>

```php
abstract public function extraFields(): array
```

Returns the list of additional fields that can be returned by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray) in addition to those listed in [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields).

This method is similar to [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields) except that the list of fields declared
by this method are not returned by default by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray). Only when a field in the list
is explicitly requested, will it be included in the result of [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray).

**Returns**

`array`: The list of expandable field names or field definitions. Please refer to [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields) on the format of the return value.

**See also**

- [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray)
- [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields)

### fields() <span id="method-fields"></span>

```php
abstract public function fields(): array
```

Returns the list of fields that should be returned by default by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray) when no specific fields are specified.

A field is a named element in the returned array by [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray).

This method should return an array of field names or field definitions.
If the former, the field name will be treated as an object property name whose value will be used
as the field value. If the latter, the array key should be the field name while the array value should be
the corresponding field definition which can be either an object property name or a PHP callable
returning the corresponding field value. The signature of the callable should be:

```
function ($model, $field) {
    // return field value
}
```

For example, the following code declares four fields:

- `email`: the field name is the same as the property name `email`;
- `firstName` and `lastName`: the field names are `firstName` and `lastName`, and their
  values are obtained from the `first_name` and `last_name` properties;
- `fullName`: the field name is `fullName`. Its value is obtained by concatenating `first_name`
  and `last_name`.

```
return [
    'email',
    'firstName' => 'first_name',
    'lastName' => 'last_name',
    'fullName' => function ($model) {
        return $model->first_name . ' ' . $model->last_name;
    },
];
```

**Returns**

`array`: The list of field names or field definitions.

**See also**

- [`toArray()`](/docs/api/yii/base/arrayable/index.md#method-toArray)

### toArray() <span id="method-toArray"></span>

```php
abstract public function toArray(array $fields = [], array $expand = [], bool $recursive = true): array
```

Converts the object into an array.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fields` | `array` | The fields that the output array should contain. Fields not specified in [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields) will be ignored. If this parameter is empty, all fields as specified in [`fields()`](/docs/api/yii/base/arrayable/index.md#method-fields) will be returned. |
| `$expand` | `array` | The additional fields that the output array should contain. Fields not specified in [`extraFields()`](/docs/api/yii/base/arrayable/index.md#method-extraFields) will be ignored. If this parameter is empty, no extra fields will be returned. |
| `$recursive` | `bool` | Whether to recursively return array representation of embedded objects. |

**Returns**

`array`: The array representation of the object