clean.php
Table of Contents
Functions
- clean() : array<string|int, mixed>
- 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>
This function allows flexible and recursive filtering of arrays. Cleaning behavior is fully customizable via bitwise flags.
Supported flags from CleanFlag
:
CleanFlag::NULLS
: Removesnull
values.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
.
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::RECURSIVE
is set. - $flags : int = CleanFlag::DEFAULT
-
A bitmask of
CleanFlag
values. Defaults toCleanFlag::DEFAULT
.
Tags
Return values
array<string|int, mixed> —The filtered array:
- Associative arrays: keys are preserved.
- Indexed arrays: automatically reindexed to remove numeric gaps.