Oihana PHP

ensureArrayPath.php

Table of Contents

Functions

ensureArrayPath()  : array<string|int, mixed>
Ensures that a given key in an array contains a sub-array, and returns it by reference.

Functions

ensureArrayPath()

Ensures that a given key in an array contains a sub-array, and returns it by reference.

& ensureArrayPath(array<string|int, mixed> &$current, string $segment) : array<string|int, mixed>

If the key does not exist or is not an array, it initializes it as an empty array. This is useful for building nested structures dynamically using references.

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

The parent array passed by reference.

$segment : string

The key to check or initialize as an array.

Tags
example
$data = [];
$sub =& ensureArrayPath($data, 'user');
$sub['name'] = 'Alice';
// $data now becomes: ['user' => ['name' => 'Alice']]
author

Marc Alcaraz (ekameleon)

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

A reference to the array stored at the specified key.


        
On this page

Search results