# ArrayAccessTrait

> ArrayAccessTrait provides the implementation for IteratorAggregate, ArrayAccess and Countable.

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

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

Used by 2 types:

- [`yii\test\ArrayFixture`](/docs/api/yii/test/arrayfixture/index.md)
- [`yii\test\BaseActiveFixture`](/docs/api/yii/test/baseactivefixture/index.md)

ArrayAccessTrait provides the implementation for [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php) and [`Countable`](https://www.php.net/manual/en/class.countable.php).

Note that ArrayAccessTrait requires the class using it contain a property named `data` which should be an array.
The data will be exposed by ArrayAccessTrait to support accessing the class object like an array.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-data"></span>`$data` | `array<array-key, mixed>` |  | `ArrayAccessTrait` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`count()`](#method-count) | Returns the number of data items. | `ArrayAccessTrait` |
| [`getIterator()`](#method-getIterator) | Returns an iterator for traversing the data. | `ArrayAccessTrait` |
| [`offsetExists()`](#method-offsetExists) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `ArrayAccessTrait` |
| [`offsetGet()`](#method-offsetGet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `ArrayAccessTrait` |
| [`offsetSet()`](#method-offsetSet) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `ArrayAccessTrait` |
| [`offsetUnset()`](#method-offsetUnset) | This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php). | `ArrayAccessTrait` |

## Method details

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

```php
public function count(): int
```

Returns the number of data items.

This method is required by Countable interface.

**Returns**

`int`: Number of data elements.

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

```php
public function getIterator(): ArrayIterator<array-key, mixed>
```

Returns an iterator for traversing the data.

This method is required by the SPL interface [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php).
It will be implicitly called when you use `foreach` to traverse the collection.

**Returns**

[`ArrayIterator`](https://www.php.net/manual/en/class.arrayiterator.php)`<array-key, mixed>`: An iterator for traversing the cookies in the collection.

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

```php
public function offsetExists(int|string $offset): bool
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to check on |

**Returns**

`bool`

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

```php
public function offsetGet(int|string $offset): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to retrieve element. |

**Returns**

`mixed`: The element at the offset, null if no element is found at the offset

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

```php
public function offsetSet(int|string $offset, mixed $item): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to set element |
| `$item` | `mixed` | The element value |

**Returns**

`mixed`

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

```php
public function offsetUnset(int|string $offset): mixed
```

This method is required by the interface [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | The offset to unset element |

**Returns**

`mixed`