# Array Built-ins

| Function | Description | OPA | Wasm | [Swift](https://github.com/open-policy-agent/swift-opa) | [Java](https://github.com/open-policy-agent/java-opa-sdk) |
| --- | --- | --- | --- | --- | --- |
| `array.concat` | `z := array.concat(x, y)`  Concatenates two arrays.  **Arguments:**  `x` (array\[any\])  the first array  `y` (array\[any\])  the second array  **Returns:**  `z` (array\[any\])  the concatenation of `x` and `y` | v0.17.0 | ✓ | 0.0.1 | 0.1.0 |
| `array.flatten` | `flattened := array.flatten(arr)`  Non-recursively unpacks array items in arr into the flattened array. Other types are appended as-is.  **Arguments:**  `arr` (array\[any\])  the array to be flattened  **Returns:**  `flattened` (array\[any\])  array flattened one level | [v1.13.0](https://github.com/open-policy-agent/opa/releases/v1.13.0) | ✓ | ✗ | 0.3.0 |
| `array.reverse` | `rev := array.reverse(arr)`  Returns the reverse of a given array.  **Arguments:**  `arr` (array\[any\])  the array to be reversed  **Returns:**  `rev` (array\[any\])  an array containing the elements of `arr` in reverse order | [v0.36.0](https://github.com/open-policy-agent/opa/releases/v0.36.0) | ✓ | 0.0.1 | 0.1.0 |
| `array.slice` | `slice := array.slice(arr, start, stop)`  Returns a slice of a given array. If `start` is greater or equal than `stop`, `slice` is `[]`.  **Arguments:**  `arr` (array\[any\])  the array to be sliced  `start` (number)  the start index of the returned slice; if less than zero, it's clamped to 0  `stop` (number)  the stop index of the returned slice; if larger than `count(arr)`, it's clamped to `count(arr)`  **Returns:**  `slice` (array\[any\])  the subslice of `array`, from `start` to `end`, including `arr[start]`, but excluding `arr[end]` | v0.17.0 | ✓ | 0.0.1 | 0.1.0 |

## Examples

### `array.concat`

`array.concat` returns a new array with the elements of the second array appended to the first. Policies use it to extend a base list — for example default registries or hosts — with extra values from input or data.

Merging a base allowlist with extra entries

`array.concat` appends one array to another. Policies often keep a fixed base list (for example default registries) and extend it with values from input or data for a particular tenant or environment.

policy.rego

```
package playallowed_registries := array.concat(data.base_registries, input.extra_registries)default allow := falseallow if {	some registry in allowed_registries	startswith(input.image, sprintf("%s/", [registry]))}deny contains msg if {	not allow	msg := sprintf(		"image %q is not from an allowed registry: %v",		[input.image, allowed_registries],	)}
```

Output

{
  "allow": false,
  "allowed\_registries": \[
    "registry.internal/prod",
    "ghcr.io/example",
    "registry.internal/staging"
  \],
  "deny": \[
    "image \\"docker.io/library/nginx:1.27\\" is not from an allowed registry: \[\\"registry.internal/prod\\", \\"ghcr.io/example\\", \\"registry.internal/staging\\"\]"
  \]
}

input.json

```
{  "extra_registries": [    "registry.internal/staging"  ],  "image": "docker.io/library/nginx:1.27"}
```

data.json

```
{  "base_registries": [    "registry.internal/prod",    "ghcr.io/example"  ]}
```

[Open in OPA Playground](https://play.openpolicyagent.org/?state=eyJpIjoie1xuICBcImV4dHJhX3JlZ2lzdHJpZXNcIjogW1xuICAgIFwicmVnaXN0cnkuaW50ZXJuYWwvc3RhZ2luZ1wiXG4gIF0sXG4gIFwiaW1hZ2VcIjogXCJkb2NrZXIuaW8vbGlicmFyeS9uZ2lueDoxLjI3XCJcbn0iLCJkIjoie1xuICBcImJhc2VfcmVnaXN0cmllc1wiOiBbXG4gICAgXCJyZWdpc3RyeS5pbnRlcm5hbC9wcm9kXCIsXG4gICAgXCJnaGNyLmlvL2V4YW1wbGVcIlxuICBdXG59IiwicCI6InBhY2thZ2UgcGxheVxuXG5hbGxvd2VkX3JlZ2lzdHJpZXMgOj0gYXJyYXkuY29uY2F0KGRhdGEuYmFzZV9yZWdpc3RyaWVzLCBpbnB1dC5leHRyYV9yZWdpc3RyaWVzKVxuXG5kZWZhdWx0IGFsbG93IDo9IGZhbHNlXG5cbmFsbG93IGlmIHtcblx0c29tZSByZWdpc3RyeSBpbiBhbGxvd2VkX3JlZ2lzdHJpZXNcblx0c3RhcnRzd2l0aChpbnB1dC5pbWFnZSwgc3ByaW50ZihcIiVzL1wiLCBbcmVnaXN0cnldKSlcbn1cblxuZGVueSBjb250YWlucyBtc2cgaWYge1xuXHRub3QgYWxsb3dcblx0bXNnIDo9IHNwcmludGYoXG5cdFx0XCJpbWFnZSAlcSBpcyBub3QgZnJvbSBhbiBhbGxvd2VkIHJlZ2lzdHJ5OiAldlwiLFxuXHRcdFtpbnB1dC5pbWFnZSwgYWxsb3dlZF9yZWdpc3RyaWVzXSxcblx0KVxufVxuIn0=)