> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# array_position

> 获取数组中某个元素的位置。

获取数组中某个元素位置，是的话返回位置，否则返回0.

## 语法

```Haskell theme={null}
array_position(any_array, any_element)
```

## 示例

```plain text theme={null}
mysql> select array_position(["apple","orange","pear"], "orange");
+-----------------------------------------------------+
| array_position(['apple','orange','pear'], 'orange') |
+-----------------------------------------------------+
|                                                   2 |
+-----------------------------------------------------+
1 row in set (0.01 sec)
```

也可以获取数组NULL的位置。

```plain text theme={null}
mysql> select array_position([1, NULL], NULL);
+--------------------------------+
| array_position([1,NULL], NULL) |
+--------------------------------+
|                              2 |
+--------------------------------+
1 row in set (0.00 sec)
```

也可以应用在多维数组中，获取某个子数组的位置，此时要求子数组元素完全匹配，包括元素排列顺序

```plain text theme={null}
mysql> select array_position([[1,2,3], [4,5,6]], [4,5,6]);
+--------------------------------------------+
| array_position([[1,2,3],[4,5,6]], [4,5,6]) |
+--------------------------------------------+
|                                          2 |
+--------------------------------------------+
1 row in set (0.00 sec)

mysql> select array_position([[1,2,3], [4,5,6]], [4,6,5]);
+--------------------------------------------+
| array_position([[1,2,3],[4,5,6]], [4,6,5]) |
+--------------------------------------------+
|                                          0 |
+--------------------------------------------+
1 row in set (0.00 sec)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.