14

If I have:

$_POST['test']

then can I use:

$request->getParameter('test');

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

5 Answers 5

17

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'];

2nd Edition :

In Symfony 6 and 7 the correct answer is

$request->request->all('test')['two'];

When you ask for array in GET call:

$request->query->all('test')['two'];
Sign up to request clarification or add additional context in comments.

Comments

10

Symfony version 6.0, the correct answer is

$request->request->all('test')['two'];

When you ask for array in GET call:

$request->query->all('test')['two'];

Comments

6

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."

1 Comment

Please note that "Finding deep items in ParameterBag::get() is deprecated since version 2.8 and will be removed in 3.0." github.com/symfony/symfony/blob/2.8/CHANGELOG-2.8.md
2
$request->getParameter('test')['two'];

Comments

1

there is a easy way to get the whole request

$var = $request->request->all()

$var will be an array. http://symfony.com/doc/current/components/http_foundation/introduction.html#accessing-request-data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.