> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/intarray/intarray-funcs-ops.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.19.1. intarray 函式與運算子

## F.19.1. `intarray` 函式與運算子 [#](#intarray-funcs-ops) <a href="#intarray-funcs-ops" id="intarray-funcs-ops"></a>

`intarray` 模組提供的函式列於[表 F.8](#intarray-func-table)，運算子列於[表 F.9](#intarray-op-table)。

### 表 F.8. `intarray` 函式 [#](#intarray-func-table) <a href="#intarray-func-table" id="intarray-func-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>icount</code> ( <code>integer\[]</code> ) → <code>integer</code></p><p>傳回陣列中的元素數量。</p><p><code>icount('{1,2,3}'::integer\[])</code> → <code>3</code></p>                                                                                                                                                                                          |
| <p><code>sort</code> ( <code>integer\[]</code>, <em><code>dir</code></em> <code>text</code> ) → <code>integer\[]</code></p><p>以遞增或遞減順序排序陣列。<em><code>dir</code></em> 必須是 <code>asc</code> 或 <code>desc</code>。</p><p><code>sort('{1,3,2}'::integer\[], 'desc')</code> → <code>{3,2,1}</code></p>                                                           |
| <p><code>sort</code> ( <code>integer\[]</code> ) → <code>integer\[]</code></p><p><code>sort\_asc</code> ( <code>integer\[]</code> ) → <code>integer\[]</code></p><p>以遞增順序排序。</p><p><code>sort(array\[11,77,44])</code> → <code>{11,44,77}</code></p>                                                                                                       |
| <p><code>sort\_desc</code> ( <code>integer\[]</code> ) → <code>integer\[]</code></p><p>以遞減順序排序。</p><p><code>sort\_desc(array\[11,77,44])</code> → <code>{77,44,11}</code></p>                                                                                                                                                                              |
| <p><code>uniq</code> ( <code>integer\[]</code> ) → <code>integer\[]</code></p><p>移除相鄰的重複項目。常與 <code>sort</code> 搭配使用，以移除所有重複項目。</p><p><code>uniq('{1,2,2,3,1,1}'::integer\[])</code> → <code>{1,2,3,1}</code></p><p><code>uniq(sort('{1,2,3,2,1}'::integer\[]))</code> → <code>{1,2,3}</code></p>                                                          |
| <p><code>idx</code> ( <code>integer\[]</code>, <em><code>item</code></em> <code>integer</code> ) → <code>integer</code></p><p>傳回第一個符合 <em><code>item</code></em> 的陣列元素索引；若沒有符合項目則傳回 0。</p><p><code>idx(array\[11,22,33,22,11], 22)</code> → <code>2</code></p>                                                                                             |
| <p><code>subarray</code> ( <code>integer\[]</code>, <em><code>start</code></em> <code>integer</code>, <em><code>len</code></em> <code>integer</code> ) → <code>integer\[]</code></p><p>擷取從位置 <em><code>start</code></em> 開始、含有 <em><code>len</code></em> 個元素的陣列部分。</p><p><code>subarray('{1,2,3,2,1}'::integer\[], 2, 3)</code> → <code>{2,3,2}</code></p> |
| <p><code>subarray</code> ( <code>integer\[]</code>, <em><code>start</code></em> <code>integer</code> ) → <code>integer\[]</code></p><p>擷取從位置 <em><code>start</code></em> 開始的陣列部分。</p><p><code>subarray('{1,2,3,2,1}'::integer\[], 2)</code> → <code>{2,3,2,1}</code></p>                                                                                   |
| <p><code>intset</code> ( <code>integer</code> ) → <code>integer\[]</code></p><p>建立單一元素陣列。</p><p><code>intset(42)</code> → <code>{42}</code></p>                                                                                                                                                                                                            |

<br>

### 表 F.9. `intarray` 運算子 [#](#intarray-op-table) <a href="#intarray-op-table" id="intarray-op-table"></a>

| <p>運算子</p><p>說明</p>                                                                                                                                        |                                                                              |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| <p><code>integer\[]</code> <code>&&</code> <code>integer\[]</code> → <code>boolean</code></p><p>陣列是否重疊（至少有一個共同元素）？</p>                                     |                                                                              |
| <p><code>integer\[]</code> <code>@></code> <code>integer\[]</code> → <code>boolean</code></p><p>左側陣列是否包含右側陣列？</p>                                          |                                                                              |
| <p><code>integer\[]</code> <code><@</code> <code>integer\[]</code> → <code>boolean</code></p><p>左側陣列是否包含於右側陣列？</p>                                         |                                                                              |
| <p><code>#</code> <code>integer\[]</code> → <code>integer</code></p><p>傳回陣列中的元素數量。</p>                                                                     |                                                                              |
| <p><code>integer\[]</code> <code>#</code> <code>integer</code> → <code>integer</code></p><p>傳回第一個符合右側引數的陣列元素索引；若沒有符合項目則傳回 0。（與 <code>idx</code> 函式相同。）</p> |                                                                              |
| <p><code>integer\[]</code> <code>+</code> <code>integer</code> → <code>integer\[]</code></p><p>將元素新增至陣列末端。</p>                                             |                                                                              |
| <p><code>integer\[]</code> <code>+</code> <code>integer\[]</code> → <code>integer\[]</code></p><p>串接陣列。</p>                                                |                                                                              |
| <p><code>integer\[]</code> <code>-</code> <code>integer</code> → <code>integer\[]</code></p><p>從陣列移除符合右側引數的項目。</p>                                         |                                                                              |
| <p><code>integer\[]</code> <code>-</code> <code>integer\[]</code> → <code>integer\[]</code></p><p>從左側陣列移除右側陣列的元素。</p>                                      |                                                                              |
| <p><code>integer\[]</code> <code>                                                                                                                          | </code> <code>integer</code> → <code>integer\[]</code></p><p>計算引數的聯集。</p>    |
| <p><code>integer\[]</code> <code>                                                                                                                          | </code> <code>integer\[]</code> → <code>integer\[]</code></p><p>計算引數的聯集。</p> |
| <p><code>integer\[]</code> <code>&</code> <code>integer\[]</code> → <code>integer\[]</code></p><p>計算引數的交集。</p>                                             |                                                                              |
| <p><code>integer\[]</code> <code>@@</code> <code>query\_int</code> → <code>boolean</code></p><p>陣列是否滿足查詢？（見下文）</p>                                         |                                                                              |
| <p><code>query\_int</code> <code>\~\~</code> <code>integer\[]</code> → <code>boolean</code></p><p>陣列是否滿足查詢？（<code>@@</code> 的交換子）</p>                      |                                                                              |

<br>

`&&`、`@>` 與 `<@` 運算子等同於 PostgreSQL 同名內建運算子，但它們僅可處理不含 NULL 的整數陣列，而內建運算子可處理任何陣列型別。這項限制使它們在許多情況下比內建運算子更快。

`@@` 與 `~~` 運算子測試陣列是否滿足*查詢*；查詢以專用資料型別 `query_int` 的值表示。*查詢*由要與陣列元素比對的整數值組成，可能使用 `&`（AND）、`|`（OR）與 `!`（NOT）運算子組合。可視需要使用括號。例如，查詢 `1&(2|3)` 符合包含 1 且包含 2 或 3 其中之一的陣列。

[返回 F.19. intarray — 操作整數陣列](https://docs.postgresql.tw/19/appendixes/contrib/intarray.md#intarray)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/intarray.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/intarray/intarray-funcs-ops.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
