Oihana PHP

MergeOption

Defines constants for the options used by the `merge` function.

Using these constants helps avoid typos and ensures consistent option handling.

Each constant corresponds to a configurable option:

  • CLEAN : Bitmask of CleanFlag constants for optional cleaning.
  • DEEP : Enable recursive (deep) merge.
  • INDEXED : Store values in sub-arrays (indexed).
  • NULLS : How to handle null values ('skip', 'keep', 'overwrite').
  • PRESERVE_KEYS : Preserve existing keys for numeric indices.
  • UNIQUE : Remove duplicates in lists.
Tags
author

Marc Alcaraz

author

Marc Alcaraz

since
1.0.0
since
1.0.8
example

Using MergeOption with merge

use oihana\core\options\MergeOption;
use function oihana\core\arrays\merge;
use oihana\core\arrays\CleanFlag;

$a = ['foo' => 'bar', 'count' => 2];
$b = ['foo' => null, 'new' => null];

$result = merge($a, $b, MergeOption::normalize([
    MergeOption::DEEP       => true,
    MergeOption::UNIQUE     => true,
    MergeOption::NULLS      => 'skip',
    MergeOption::CLEAN      => CleanFlag::DEFAULT
]));

Table of Contents

Constants

CLEAN  = 'clean'
Option key to defines the Bitmask of CleanFlag constants for optional cleaning.
DEEP  = 'deep'
Option key to recursive merge.
INDEXED  = 'indexed'
Option key to store values in sub-array
NULLS  = 'nulls'
Option key to defines the "null" behaviors ('skip'|'keep'|'overwrite')
PRESERVE_KEYS  = 'preserveKeys'
Option key to preserve numeric keys.
UNIQUE  = 'unique'
Option key to avoid duplicates in lists.

Methods

normalize()  : array<string|int, mixed>
Normalize an options array for merge function.

Constants

CLEAN

Option key to defines the Bitmask of CleanFlag constants for optional cleaning.

public mixed CLEAN = 'clean'

DEEP

Option key to recursive merge.

public mixed DEEP = 'deep'

Type: bool

INDEXED

Option key to store values in sub-array

public mixed INDEXED = 'indexed'

NULLS

Option key to defines the "null" behaviors ('skip'|'keep'|'overwrite')

public mixed NULLS = 'nulls'

PRESERVE_KEYS

Option key to preserve numeric keys.

public mixed PRESERVE_KEYS = 'preserveKeys'

UNIQUE

Option key to avoid duplicates in lists.

public mixed UNIQUE = 'unique'

Methods

normalize()

Normalize an options array for merge function.

public static normalize([array<string|int, mixed>|null $options = [] ]) : array<string|int, mixed>

Fills in defaults for missing keys and ensures consistent option names.

Parameters
$options : array<string|int, mixed>|null = []

User-provided options

Tags
example
$opts = MergeOption::normalize
([
    MergeOption::DEEP => true,
    MergeOption::NULLS => NullsOption::KEEP // "keep"
]);

// Result:
// [
//   'deep'         => true,
//   'indexed'      => false,
//   'unique'       => false,
//   'clean'        => null,
//   'preserveKeys' => true,
//   'nulls'        => 'keep'
// ]
Return values
array<string|int, mixed>

Normalized options with default values


        
On this page

Search results