# ArrayExpression

> Class ArrayExpression represents an array SQL expression.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › ArrayExpression

| class | `yii\db\ArrayExpression` |
| --- | --- |
| Implements | [`ExpressionInterface`](/docs/api/yii/db/expressioninterface/index.md), [`ArrayAccess`](https://www.php.net/manual/en/class.arrayaccess.php), [`Countable`](https://www.php.net/manual/en/class.countable.php), [`IteratorAggregate`](https://www.php.net/manual/en/class.iteratoraggregate.php) |
| Since | 2.0.14 |
| Source | [`framework/db/ArrayExpression.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/ArrayExpression.php#L33) |

Class ArrayExpression represents an array SQL expression.

Expressions of this type can be used in conditions as well:

```
$query->andWhere(['@>', 'items', new ArrayExpression([1, 2, 3], 'integer')])
```

which, depending on DBMS, will result in a well-prepared condition. For example, in
PostgreSQL it will be compiled to `WHERE "items" @> ARRAY[1, 2, 3]::integer[]`.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | ArrayExpression constructor. | `ArrayExpression` |
| [`count()`](#method-count) | Count elements of an object | `ArrayExpression` |
| [`getDimension()`](#method-getDimension) |  | `ArrayExpression` |
| [`getIterator()`](#method-getIterator) | Retrieve an external iterator | `ArrayExpression` |
| [`getType()`](#method-getType) |  | `ArrayExpression` |
| [`getValue()`](#method-getValue) |  | `ArrayExpression` |
| [`offsetExists()`](#method-offsetExists) | Whether a offset exists | `ArrayExpression` |
| [`offsetGet()`](#method-offsetGet) | Offset to retrieve | `ArrayExpression` |
| [`offsetSet()`](#method-offsetSet) | Offset to set | `ArrayExpression` |
| [`offsetUnset()`](#method-offsetUnset) | Offset to unset | `ArrayExpression` |

## Method details

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

```php
public function __construct(array|yii\db\QueryInterface|mixed $value, string|null $type = null, int $dimension = 1)
```

ArrayExpression constructor.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$value` | `array\|`[`QueryInterface`](/docs/api/yii/db/queryinterface/index.md)`\|mixed` | The array content. Either represented as an array of values or a Query that returns these values. A single value will be considered as an array containing one element. |
| `$type` | `string\|null` | The type of the array elements. Defaults to `null` which means the type is not explicitly specified. In case when type is not specified explicitly and DBMS can not guess it from the context, SQL error will be raised. |
| `$dimension` | `int` | The number of indices needed to select an element |

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

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

Count elements of an object

**Returns**

`int`: The custom count as an integer. &lt;/p> &lt;p> The return value is cast to an integer.

**Since**

2.0.14

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

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

**Returns**

`int`: The number of indices needed to select an element

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

```php
public function getIterator(): Traversable
```

Retrieve an external iterator

**Returns**

[`Traversable`](https://www.php.net/manual/en/class.traversable.php): An instance of an object implementing &lt;b>Iterator&lt;/b> or &lt;b>Traversable&lt;/b>

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when ArrayExpression contains QueryInterface object

**Since**

2.0.14.1

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

```php
public function getType(): string|null
```

**Returns**

`string|null`

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

```php
public function getValue(): array|mixed|yii\db\QueryInterface
```

**Returns**

`array|mixed|`[`QueryInterface`](/docs/api/yii/db/queryinterface/index.md)

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

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

Whether a offset exists

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | &lt;p> An offset to check for. &lt;/p> |

**Returns**

`bool`: True on success or false on failure. &lt;/p> &lt;p> The return value will be casted to boolean if non-boolean was returned.

**Since**

2.0.14

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

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

Offset to retrieve

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | &lt;p> The offset to retrieve. &lt;/p> |

**Returns**

`mixed`: Can return all value types.

**Since**

2.0.14

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

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

Offset to set

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | &lt;p> The offset to assign the value to. &lt;/p> |
| `$value` | `mixed` | &lt;p> The value to set. &lt;/p> |

**Since**

2.0.14

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

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

Offset to unset

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `int\|string` | &lt;p> The offset to unset. &lt;/p> |

**Since**

2.0.14