Oihana PHP

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 : Removes null 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 to CleanFlag::DEFAULT.

Tags
example

1. Basic cleaning: remove nulls and empty strings

use oihana\core\arrays\{clean, CleanFlag};

$data   = ['foo', '', null, 'bar'];
$result = clean($data, CleanFlag::NULLS | CleanFlag::EMPTY);
// ['foo', 'bar']

2. Remove whitespace-only strings as well

$data   = ['foo', '   ', '', null, 'bar'];
$result = clean($data, CleanFlag::NULLS | CleanFlag::EMPTY | CleanFlag::TRIM);
// ['foo', 'bar']

3. Recursive cleaning of nested arrays

$data = [
    'users' => [
        ['name' => '', 'email' => 'bob@example.com'],
        ['name' => 'Alice', 'email' => '']
    ]
];

$result = clean($data, CleanFlag::RECURSIVE | CleanFlag::EMPTY);
// [
//     'users' => [
//         ['email' => 'bob@example.com'],
//         ['name' => 'Alice']
//     ]
// ]

4. Remove empty arrays after recursive cleaning

$data = [
    'group1' => [],
    'group2' => [['name' => 'Alice'], []]
];

$result = clean($data, CleanFlag::RECURSIVE | CleanFlag::EMPTY_ARR);
// [
//     'group2' => [['name' => 'Alice']]
// ]

5. Remove all falsy values at once

$data   = [0, '', null, false, 'ok', [], '0'];
$result = clean($data, CleanFlag::FALSY);
// ['ok']

6. Full cleaning using default flags

$result = clean($data);
// Equivalent to CleanFlag::DEFAULT
see
CleanFlag

For available cleaning flags and default behavior.

see
isIndexed()

To determine if an array is numerically indexed.

author

Marc Alcaraz

since
1.0.6
Return values
array<string|int, mixed>

The filtered array:

  • Associative arrays: keys are preserved.
  • Indexed arrays: automatically reindexed to remove numeric gaps.

        
On this page

Search results