Oihana PHP

compress.php

Table of Contents

Functions

compress()  : array<string|int, mixed>
Compress the given array by removing entries that match specified conditions.

Functions

compress()

Compress the given array by removing entries that match specified conditions.

compress(array<string|int, mixed> $array[, array{clone?: bool, conditions?: callable|callable[], excludes?: string[], recursive?: bool, depth?: int|null, removeKeys?: string[], throwable?: bool}|null $options = [] ][, int $currentDepth = 0 ]) : array<string|int, mixed>

This function traverses an array and removes elements according to the provided configuration. It can work recursively on nested arrays and objects, with optional depth limitation and key-based exclusions.

Useful for cleaning associative arrays (e.g., form data, object exports) by removing nulls, empty strings, or other unwanted values.

Parameters
$array : array<string|int, mixed>

The input array to compress.

$options : array{clone?: bool, conditions?: callable|callable[], excludes?: string[], recursive?: bool, depth?: int|null, removeKeys?: string[], throwable?: bool}|null = []

Optional configuration.

$currentDepth : int = 0

Internal counter used to track recursion depth.

Tags
throws
InvalidArgumentException

If invalid callbacks are provided and 'throwable' is true.

example

Basic cleanup of null values

use function oihana\core\arrays\compress;

$data = [ 'id' => 1 , 'name' => 'hello' , 'desc' => null ];
$clean = compress($data);

// Result: [ 'id' => 1 , 'name' => 'hello' ]
example

Excluding a key from filtering

$data = [ 'id' => 1 , 'name' => null ];
$clean = compress($data, [ 'excludes' => ['name'] ]);

// Result: [ 'id' => 1 , 'name' => null ]
example

Removing null or empty strings

$data = [ 'a' => '' , 'b' => 0 , 'c' => null ];

$clean = compress($data, [
    'conditions' => [
        fn($v) => $v === null,
        fn($v) => is_string($v) && $v === ''
    ]
]);

// Result: [ 'b' => 0 ]
example

Recursive compression with depth limit

$nested = [
    'id'   => 2,
    'meta' => [ 'created' => null, 'tags' => [] ],
];

$clean = compress($nested, [
    'conditions' => fn($v) => $v === [] || $v === null,
    'recursive'  => true,
    'depth'      => 1
]);

// Result: [ 'id' => 2 ]
example

Mixing arrays and objects

use function oihana\core\objects\compress as compressObject;

$data = [
    'user' => (object)[ 'id' => 1 , 'tmp' => null ],
    'tags' => [ 'a' => null , 'b' => 'keep' ],
];

$clean = compress($data, [
    'conditions' => fn($v) => $v === null,
    'recursive'  => true,
]);

// Result: [ 'user' => { "id":1 }, 'tags' => [ 'b' => 'keep' ] ]
example

Cloning input before compression

$original = [ 'x' => null , 'y' => 5 ];
$copy     = compress($original, [ 'clone' => true ]);

// $original remains unchanged
// $copy == [ 'y' => 5 ]
author

Marc Alcaraz

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

The compressed array (or its clone if clone=true).


        
On this page

Search results