Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$request->getParameter with array - Symfony

Tags:

php

symfony1

If I have:

$_POST['test']

then can I use:

$request->getParameter('test');

But how can I use this if I have $_POST['test']['two']?

like image 703
Gryz Oshea Avatar asked Nov 01 '11 12:11

Gryz Oshea


3 Answers

Now only one way do to it:

$arr = $request->getParameter('test');
$two = $arr['two'];

Edited:

In PHP 5.4 you can do it $request->getParameter('test')['two'];

like image 152
Alex Pliutau Avatar answered Nov 19 '22 12:11

Alex Pliutau


$request->getParameter('test')['two'];
like image 41
Kamil Avatar answered Nov 19 '22 11:11

Kamil


As of Symfony 2, there's even a prettier solution to get array values with the Symfony Request:

$request->get("test[two]", null, true)

The third parameter of get(), $deep, is false by default and decides whether you can access array keys.

See the documentation of the ParameterBag:

boolean $deep: If true, a path like foo[bar] will find deeper items

http://api.symfony.com/2.0/Symfony/Component/HttpFoundation/ParameterBag.html#method_get

As antongorodezkiz pointed out in his comment to this answer, please note that this "is deprecated since version 2.8 and will be removed in 3.0."

like image 6
Andreas Fröwis Avatar answered Nov 19 '22 11:11

Andreas Fröwis