Oihana PHP System

KeyValueTrait

Trait KeyValueTrait

Provides utility methods to get and set key-value pairs from a document, whether the document is an array or an object.

This trait is useful when working with dynamic data structures where the type (array or object) is not guaranteed.

Methods:

  • getKeyValue(): Retrieve a value by key from an array or object.
  • setKeyValue(): Set a value by key into an array or object.

Example usage:

use oihana\traits\KeyValueTrait;

$data = ['name' => 'Alice'];
$value = $this->getKeyValue($data, 'name'); // returns 'Alice'

$data = $this->setKeyValue($data, 'age', 30);
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Methods

getKeyValue()  : mixed
Gets the property value of with a specific key name in a document.
setKeyValue()  : array<string|int, mixed>|object
Set the property value of with a specific key name in a document.

Methods

getKeyValue()

Gets the property value of with a specific key name in a document.

public getKeyValue(array<string|int, mixed>|object $document, string $key[, bool|null $isArray = null ]) : mixed
Parameters
$document : array<string|int, mixed>|object
$key : string
$isArray : bool|null = null

Set automatically the document type if null or indicates manually if the document is an array (true) or an object (false).

setKeyValue()

Set the property value of with a specific key name in a document.

public setKeyValue(array<string|int, mixed>|object $document, string $key, mixed $value[, bool|null $isArray = null ]) : array<string|int, mixed>|object
Parameters
$document : array<string|int, mixed>|object
$key : string
$value : mixed
$isArray : bool|null = null

Set automatically the document type if null or indicates manually if the document is an array (true) or an object (false).

Return values
array<string|int, mixed>|object

        
On this page

Search results