Oihana PHP System

getBodyParams.php

Table of Contents

Functions

getBodyParams()  : array<string|int, mixed>
Retrieves multiple parameters from the HTTP request body.

Functions

getBodyParams()

Retrieves multiple parameters from the HTTP request body.

getBodyParams(ServerRequestInterface|null $request[, array<string|int, mixed> $names = [] ]) : array<string|int, mixed>

This helper extracts one or more values from the parsed request body ($request->getParsedBody()), supporting dot notation for nested structures (e.g. 'user.address.city').

Each requested key from $names is resolved recursively via hasKeyValue() and getKeyValue(), and reassembled into a new associative array using setKeyValue(). The request body is first normalized into a pure associative array using toAssociativeArray(), ensuring compatibility with both array and stdClass payloads.

If the request is null, the function returns null. If none of the requested keys exist, an empty array is returned.

Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance. If null, no extraction is performed.

$names : array<string|int, mixed> = []

A list of parameter names (keys or dot-notated paths) to extract.

Tags
example

Retrieve multiple flat parameters:

// POST body: ['name' => 'Alice', 'age' => 30]
$params = getBodyParams($request, ['name', 'age']);
// ['name' => 'Alice', 'age' => 30]

Retrieve nested parameters with dot notation:

// POST body: ['user' => ['profile' => ['email' => 'a@b.c', 'active' => true]]]
$params = getBodyParams($request, ['user.profile.email', 'user.profile.active']);
// ['user' => ['profile' => ['email' => 'a@b.c', 'active' => true]]]

Handle missing keys or null request:

getBodyParams(null, ['foo', 'bar']);  // []
getBodyParams($request, ['unknown']); // []
author

Marc Alcaraz (ekameleon)

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

An associative array of extracted values. Nested keys are preserved according to dot notation.


        
On this page

Search results