clean.php
Table of Contents
Functions
- clean() : array<string|int, 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<string|int, mixed> $array = [] ][, int $flags = CleanFlag::DEFAULT ]) : array<string|int, 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: Trims strings and treats whitespace-only strings as empty.CleanFlag::EMPTY_ARR: Removes empty arrays (after recursive cleaning).CleanFlag::RECURSIVE: Cleans nested arrays recursively.CleanFlag::FALSY: Removes all PHP falsy values (null,'',0,0.0,'0',false,[]).CleanFlag::MAIN: Shortcut for enabling all the main flags:NULLS | EMPTY | EMPTY_ARR | TRIM.CleanFlag::RETURN_NULL: Returns null if the final array is empty.
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<string|int, 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<string|int, 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.