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

# array_map

> array_map()是高阶函数，使用Lambda函数对数组进行转换并返回新数组。

array\_map() 是支持 Lambda 表达式的高阶函数。用于将输入的 `arr1`，`arr2` 等数组按照 `lambda_function` 进行转换，输出一个新的数组。有关 Lambda 表达式的详细信息，参见 [Lambda expression](/documents/cn/4.2/sql-reference/sql-functions/Lambda_expression.md)。该函数从 2.5 版本开始支持。

该函数支持多维数组，可以通过嵌套 Lambda 函数来捕获变量。

array\_map() 别名为 transform()。

## 语法

```Haskell theme={null}
array_map(lambda_function, arr1, arr2...)
array_map(arr1, arr2..., lambda_function)
```

## 注意事项

* Lambda 函数只能作为高阶函数的第一个或者最后一个参数。不管在第一个位置还是最后一个位置，都对计算过程没有影响。
* 数组参数的数目必须等于 lambda 函数中的参数数目，否则报错。
* 所有输入数组的元素个数必须相同，否则报错。

## 示例

示例一：给 array \[1,2,3] 内的每个元素增加 100。

```SQL theme={null}
select array_map(x -> x+100,[1,2,3]);
+------------------------------------+
| array_map(x -> x + 100, [1, 2, 3]) |
+------------------------------------+
| [101,102,103]                      |
+------------------------------------+
```

示例二：将 array1 \[1,2,3] 与 array2 \[11,12,13] 对应位置的元素相加。

```SQL theme={null}
select array_map((x,y) -> x + y, [1,2,3], [11,12,13]);
+-----------------------------------------------------+
| array_map((x, y) -> x + y, [1, 2, 3], [11, 12, 13]) |
+-----------------------------------------------------+
| [12,14,16]                                          |
+-----------------------------------------------------+
```

示例三：通过条件判断，如果 x 的元素大于 1.5，则对应元素位置返回 log(x)；如果 x 的元素小于等于1.5，则对应元素位置返回 x+y。

```SQL theme={null}
select array_map((x,y) -> if(x>1.5,log(x),x+y), [1,2,3], [11,12,13]);
+--------------------------------------------------------------------------+
| array_map((x, y) -> if(x > 1.5, log(x), x + y), [1, 2, 3], [11, 12, 13]) |
+--------------------------------------------------------------------------+
| [12,0.6931471805599453,1.0986122886681098]                               |
+--------------------------------------------------------------------------+
```

示例四：通过 array\_map() 来捕获变量。对应返回 1 的位置即为满足条件的位置。

```SQL theme={null}
-- 示例表：last_avg 记录每个学生上一次的平均成绩，score 记录这一次三门考试的成绩。
+------+----------+------------+
| id   | last_avg | score      |
+------+----------+------------+
|    1 |       55 | [50,60,70] |
|    2 |       73 | [70,65,75] |
|    3 |       89 | [88,92,90] |
+------+----------+------------+

-- 找到本次考试成绩中高于上次考试平均分的科目。
select array_map(x -> x > last_avg, score) from test_tbl;
+--------------------------------------+
| array_map(x -> x > last_avg, score)  |
+--------------------------------------+
| [0,1,1]                              |
| [0,0,1]                              |
| [0,1,1]                              |
+--------------------------------------+
```

示例五：Lambda 函数的嵌套。

```SQL theme={null}
select array_map(x -> array_map(x->x+100, x),[[1,2.3],[4,3,2]]);
+-------------------------------------------------------------------+
| array_map(x -> array_map(x -> x + 100, x), [[1, 2.3], [4, 3, 2]]) |
+-------------------------------------------------------------------+
| [[101,102.3],[104,103,102]]                                       |
+-------------------------------------------------------------------+
```

示例六：Lambda 函数的参数个数和输入的数组个数不一致（函数需要 1 个数组，但输入了 2 个数组），返回报错。

```SQL theme={null}
select array_map(x -> x,[1],[2,4]);
ERROR 1064 (HY000): Lambda arguments should equal to lambda input arrays.
```


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