compress.php
Table of Contents
Functions
- compress() : array<string|int, mixed>
- Compresses the given array by removing values that match one or more conditions.
Functions
compress()
Compresses the given array by removing values that match one or more conditions.
compress(array<string|int, mixed> $array[, array<string|int, mixed>|null $options = [] ][, int $currentDepth = 0 ]) : array<string|int, mixed>
Useful for cleaning up associative arrays (e.g., from form submissions or object exports) by removing nulls, empty strings, or other unwanted values. Supports recursion into nested arrays or objects.
Parameters
- $array : array<string|int, mixed>
-
The input array to compress.
- $options : array<string|int, mixed>|null = []
-
Optional configuration:
- clone (bool) If
true
, works on a cloned copy of the array. Original remains unchanged. (default: false) - conditions (callable|array
) One or more callbacks: (mixed $value): bool
. If any condition returnstrue
, the value is removed. ( default:fn($v) => is_null($v)
)* - excludes (string[]) List of keys to exclude from filtering, even if matched by a condition.
- recursive (bool) Whether to recursively compress nested arrays or objects. (default: true)
- depth (int|null) Maximum depth for recursion.
null
means no limit. - throwable (bool) If
true
, throwsInvalidArgumentException
for invalid conditions. (default: true)
- clone (bool) If
- $currentDepth : int = 0
-
(Internal) Tracks current recursion depth. You usually don’t need to set this.
Tags
Return values
array<string|int, mixed> —The compressed array (or its clone if clone=true
).