Oihana PHP

resolveReferencePath.php

Table of Contents

Functions

resolveReferencePath()  : mixed
Navigates to the parent container of the last key segment in a nested structure.

Functions

resolveReferencePath()

Navigates to the parent container of the last key segment in a nested structure.

& resolveReferencePath(array<string|int, mixed>|object &$document, array<string|int, mixed> $keys, bool $isArray) : mixed

This function traverses through nested arrays or objects based on the provided key path, ensuring that each intermediate level exists. It returns a reference to the parent container that should contain the final key segment.

It is typically used to prepare for read/write operations on nested values.

Requires external helpers:

  • ensureArrayPath() (creates intermediate arrays)
  • ensureObjectPath() (creates intermediate objects)
Parameters
$document : array<string|int, mixed>|object

The root document to navigate through (passed by reference).

$keys : array<string|int, mixed>

The exploded path as array segments (e.g. ['user', 'profile', 'name']).

$isArray : bool

If true, treat the structure as arrays; if false, as objects.

Tags
example
$doc = [];
$keys = ['user', 'name'];
$ref = &resolveReferencePath($doc, $keys, true);
$ref['name'] = 'John';
// $doc is now: ['user' => ['name' => 'John']]
$doc = new stdClass();
$keys = ['config', 'theme'];
$ref = &resolveReferencePath($doc, $keys, false);
$ref->theme = 'dark';
// $doc is now: (object)['config' => (object)['theme' => 'dark']]
$doc = ['settings' => ['ui' => []]];
$keys = ['settings', 'ui', 'color'];
$ref = &resolveReferencePath($doc, $keys, true);
$ref['color'] = 'blue';
// $doc is now: ['settings' => ['ui' => ['color' => 'blue']]]
see
ensureArrayPath()
see
ensureObjectPath()
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
mixed

Reference to the container (array or object) that should hold the final key.


        
On this page

Search results