Oihana PHP System

getParamArray.php

Table of Contents

Functions

getParamArray()  : array<string|int, mixed>|null
Retrieves a parameter from the HTTP request and ensures it is an array.

Functions

getParamArray()

Retrieves a parameter from the HTTP request and ensures it is an array.

getParamArray(ServerRequestInterface|null $request, string $name[, array<string|int, mixed> $args = [] ][, array<string|int, mixed>|null $defaultValue = null ][, string $strategy = HttpParamStrategy::BOTH ][, bool $throwable = false ]) : array<string|int, mixed>|null

This helper calls getParam() and checks the returned value:

  • If the value is an array, it is returned.
  • Otherwise, the $defaultValue is returned.
  • If $throwable is true, a NotFoundException may be thrown by getParam().
Parameters
$request : ServerRequestInterface|null

The PSR-7 server request instance.

$name : string

The parameter name or dot-notated path.

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

Optional default values passed to getParam().

$defaultValue : array<string|int, mixed>|null = null

Value returned if the parameter is missing or not an array. Default is null.

$strategy : string = HttpParamStrategy::BOTH

Which source to search: HttpParamStrategy::BOTH|QUERY|BODY. Default is BOTH.

$throwable : bool = false

Whether to throw a NotFoundException if parameter is missing. Default false.

Tags
throws
NotFoundException

If $throwable is true and the parameter is not found.

example
// Query string: ?filters[status]=active&filters[roles][]=admin
$filters = getParamArray($request, 'filters', [], ['status' => 'all']);
example
// Body: ['user' => ['roles' => ['editor', 'admin']]]
$roles = getParamArray($request, 'user.roles', [], []);
example
// Non-array value, returns default
$tags = getParamArray($request, 'tags', [], ['default']);
author

Marc Alcaraz (ekameleon)

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

The parameter value if it is an array, otherwise $defaultValue or null.


        
On this page

Search results