clean.php
Table of Contents
Functions
- clean() : array<int|string, mixed>|null
- Cleans an array by removing unwanted values based on bitwise flags from the `CleanFlag` enum.
Functions
clean()
Cleans an array by removing unwanted values based on bitwise flags from the `CleanFlag` enum.
clean([array<int|string, mixed> $array = [] ][, int $flags = CleanFlag::DEFAULT ]) : array<int|string, mixed>|null
This function allows flexible and recursive filtering of arrays. Cleaning behavior is fully customizable via bitwise flags.
Supported flags from CleanFlag:
CleanFlag::NULLS: Removesnullvalues.CleanFlag::EMPTY: Removes empty strings ('').CleanFlag::TRIM: Modifier ofEMPTY: whitespace-only strings count as empty. Has no effect on its own, and never alters the values it keeps.CleanFlag::EMPTY_ARR: Removes empty arrays (after recursive cleaning).CleanFlag::RECURSIVE: Cleans nested arrays recursively.CleanFlag::FALSY: Removes every falsy scalar (null,'',0,0.0,'0',false). Short-circuitsNULLS/EMPTY/TRIM, and never applies to arrays — combine it withEMPTY_ARRto also discard[].CleanFlag::MAIN: Shortcut for enabling all the main flags:NULLS | EMPTY | EMPTY_ARR | TRIM.CleanFlag::RETURN_NULL: Returns null whenever nothing is left, including when the input was already empty. Applies to the outermost call only : a nested array that cleans to empty stays[].
Default behavior
Using CleanFlag::DEFAULT is equivalent to enabling:
CleanFlag::NULLS | CleanFlag::EMPTY | CleanFlag::TRIM | CleanFlag::EMPTY_ARR | CleanFlag::RECURSIVE
That means by default, clean() removes:
null- empty strings
- strings containing only spaces/tabs/newlines
- empty arrays
- and also cleans nested arrays recursively.
Parameters
- $array : array<int|string, mixed> = []
-
The input array to clean. Nested arrays are processed only if
CleanFlag::RECURSIVEis set. - $flags : int = CleanFlag::DEFAULT
-
A bitmask of
CleanFlagvalues. Defaults toCleanFlag::DEFAULT.
Tags
Return values
array<int|string, mixed>|null —The filtered array:
- Associative arrays: keys are preserved.
- Indexed arrays: automatically reindex to remove numeric gaps.
- null if the final array is empty and the CleanFlag::RETURN_NULL option is used.