# ArrayDataProvider

> ArrayDataProvider implements a data provider based on a data array.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [data](/docs/api/yii/data/index.md) › ArrayDataProvider

| class | `yii\data\ArrayDataProvider` |
| --- | --- |
| Extends | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md), [`DataProviderInterface`](/docs/api/yii/data/dataproviderinterface/index.md) |
| Since | 2.0 |
| Source | [`framework/data/ArrayDataProvider.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/data/ArrayDataProvider.php#L54) |

1 subclass:

- [`yii\debug\models\search\QueryRowDataProvider`](/docs/extensions/debug/reference/models/search/queryrowdataprovider/index.md)

ArrayDataProvider implements a data provider based on a data array.

The [`$allModels`](/docs/api/yii/data/arraydataprovider/index.md#property-allModels) property contains all data models that may be sorted and/or paginated.
ArrayDataProvider will provide the data after sorting and/or pagination.
You may configure the [`$sort`](/docs/api/yii/data/basedataprovider/index.md#property-sort) and [`$pagination`](/docs/api/yii/data/basedataprovider/index.md#property-pagination) properties to
customize the sorting and pagination behaviors.

Elements in the [`$allModels`](/docs/api/yii/data/arraydataprovider/index.md#property-allModels) array may be either objects (e.g. model objects)
or associative arrays (e.g. query results of DAO).
Make sure to set the [`$key`](/docs/api/yii/data/arraydataprovider/index.md#property-key) property to the name of the field that uniquely
identifies a data record or false if you do not have such a field.

Compared to [`ActiveDataProvider`](/docs/api/yii/data/activedataprovider/index.md), ArrayDataProvider could be less efficient
because it needs to have [`$allModels`](/docs/api/yii/data/arraydataprovider/index.md#property-allModels) ready.

ArrayDataProvider may be used in the following way:

```
$query = new Query;
$provider = new ArrayDataProvider([
    'allModels' => $query->from('post')->all(),
    'sort' => [
        'attributes' => ['id', 'username', 'email'],
    ],
    'pagination' => [
        'pageSize' => 10,
    ],
]);
// get the posts in the current page
$posts = $provider->getModels();
```

Note: if you want to use the sorting feature, you must configure the [`$sort`](/docs/api/yii/data/basedataprovider/index.md#property-sort) property
so that the provider knows which columns can be sorted.

For more details and usage information on ArrayDataProvider, see the [guide article on data providers](/docs/guide/output-data-providers/index.md).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| [`$allModels`](#property-allModels) | `array` | The data that is not paginated or sorted. Default: `null`. | `ArrayDataProvider` |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-count"></span>[`$count`](/docs/api/yii/data/basedataprovider/index.md#property-count) read-only | `int` | The number of data models in the current page. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="property-id"></span>[`$id`](/docs/api/yii/data/basedataprovider/index.md#property-id) | `string\|null` | An ID that uniquely identifies the data provider among all data providers. Default: `null`. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| [`$key`](#property-key) | `string\|array\|callable\|null` | The column that is used as the key of the data models. Default: `null`. | `ArrayDataProvider` |
| <span id="property-keys"></span>[`$keys`](/docs/api/yii/data/basedataprovider/index.md#property-keys) | `array` | The list of key values corresponding to [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models). | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| [`$modelClass`](#property-modelClass) | `string` | The name of the [Model](/docs/api/yii/base/model/index.md) class that will be represented. Default: `null`. Available since 2.0.9. | `ArrayDataProvider` |
| <span id="property-models"></span>[`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models) | `array` | The list of data models in the current page. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="property-pagination"></span>[`$pagination`](/docs/api/yii/data/basedataprovider/index.md#property-pagination) | `array\|`[`Pagination`](/docs/api/yii/data/pagination/index.md)`\|false` | The pagination to be used by this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="property-sort"></span>[`$sort`](/docs/api/yii/data/basedataprovider/index.md#property-sort) | `array\|`[`Sort`](/docs/api/yii/data/sort/index.md)`\|bool` | The sort definition to be used by this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="property-totalCount"></span>[`$totalCount`](/docs/api/yii/data/basedataprovider/index.md#property-totalCount) | `int` | Total number of possible data models. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/baseobject/index.md#method-__construct) | Constructor. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getCount"></span>[`getCount()`](/docs/api/yii/data/basedataprovider/index.md#method-getCount) | Returns the number of data models in the current page. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-getKeys"></span>[`getKeys()`](/docs/api/yii/data/basedataprovider/index.md#method-getKeys) | Returns the key values associated with the data models. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-getModels"></span>[`getModels()`](/docs/api/yii/data/basedataprovider/index.md#method-getModels) | Returns the data models in the current page. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-getPagination"></span>[`getPagination()`](/docs/api/yii/data/basedataprovider/index.md#method-getPagination) | Returns the pagination object used by this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-getSort"></span>[`getSort()`](/docs/api/yii/data/basedataprovider/index.md#method-getSort) | Returns the sorting object used by this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-getTotalCount"></span>[`getTotalCount()`](/docs/api/yii/data/basedataprovider/index.md#method-getTotalCount) | Returns the total number of data models. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-init"></span>[`init()`](/docs/api/yii/data/basedataprovider/index.md#method-init) | Initializes the object. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-prepare"></span>[`prepare()`](/docs/api/yii/data/basedataprovider/index.md#method-prepare) | Prepares the data models and keys. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-refresh"></span>[`refresh()`](/docs/api/yii/data/basedataprovider/index.md#method-refresh) | Refreshes the data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-setKeys"></span>[`setKeys()`](/docs/api/yii/data/basedataprovider/index.md#method-setKeys) | Sets the key values associated with the data models. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-setModels"></span>[`setModels()`](/docs/api/yii/data/basedataprovider/index.md#method-setModels) | Sets the data models in the current page. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-setPagination"></span>[`setPagination()`](/docs/api/yii/data/basedataprovider/index.md#method-setPagination) | Sets the pagination for this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-setSort"></span>[`setSort()`](/docs/api/yii/data/basedataprovider/index.md#method-setSort) | Sets the sort definition for this data provider. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-setTotalCount"></span>[`setTotalCount()`](/docs/api/yii/data/basedataprovider/index.md#method-setTotalCount) | Sets the total number of data models. | [`BaseDataProvider`](/docs/api/yii/data/basedataprovider/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`prepareKeys()`](#method-prepareKeys) protected | Prepares the keys associated with the currently available data models. | `ArrayDataProvider` |
| [`prepareModels()`](#method-prepareModels) protected | Prepares the data models that will be made available in the current page. | `ArrayDataProvider` |
| [`prepareTotalCount()`](#method-prepareTotalCount) protected | Returns a value indicating the total number of data models in this data provider. | `ArrayDataProvider` |
| [`sortModels()`](#method-sortModels) protected | Sorts the data models according to the given sort definition. | `ArrayDataProvider` |

## Property details

### $allModels <span id="property-allModels"></span>

```php
public array $allModels = null
```

The data that is not paginated or sorted.

When pagination is enabled,
this property usually contains more elements than [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models).
The array elements must use zero-based integer keys.

### $key <span id="property-key"></span>

```php
public string|array|callable|null $key = null
```

The column that is used as the key of the data models.

This can be either a column name, a dot-separated path, an array of keys, or a callable
that returns the key value of a given data model.
If this is not set, the index of the [`$models`](/docs/api/yii/data/basedataprovider/index.md#property-models) array will be used.

### $modelClass <span id="property-modelClass"></span>

```php
public string $modelClass = null
```

The name of the [Model](/docs/api/yii/base/model/index.md) class that will be represented.

This property is used to get columns' names.

**Since**

2.0.9

## Method details

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

```php
protected function prepareKeys(mixed $models): array
```

Prepares the keys associated with the currently available data models.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$models` | `mixed` | The available data models |

**Returns**

`array`: The keys

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

```php
protected function prepareModels(): array
```

Prepares the data models that will be made available in the current page.

**Returns**

`array`: The available data models

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

```php
protected function prepareTotalCount(): int
```

Returns a value indicating the total number of data models in this data provider.

**Returns**

`int`: Total number of data models in this data provider.

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

```php
protected function sortModels(array $models, yii\data\Sort $sort): array
```

Sorts the data models according to the given sort definition.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$models` | `array` | The models to be sorted |
| `$sort` | [`Sort`](/docs/api/yii/data/sort/index.md) | The sort definition |

**Returns**

`array`: The sorted data models