0

In my Symfony project, I have requested an URL with query params, one added through array of same key value and one of type string returning me:

http://localhost/php-api/v1/examples/query?array=1&array=2&array=3&property=aa

I was referring to the DOCS but still did not get the solution I was looking for. In every way, the array key is always returning just the last one:

$request->query->all();

Get's me:

Array
(
    [array] => 3
    [property] => aa
)

I also did try other methods from the Request class, but non of them make it return all query params.

Does anyone know how to solve this?

Output I want:

{
"array": [
    "1",
    "2",
    "3"
  ],
  "property": "aa"
}
Olivier
  • 13,283
  • 1
  • 8
  • 24
monaxi
  • 67
  • 5
  • 2
    That is not an array in the query string. An array would look like this _.../query?array[]=1&array[]=2&array[]=3&property=aa_. – lukas.j Aug 27 '23 at 07:32
  • Thanks. Edited the question. Can you still help? @lukas.j – monaxi Aug 27 '23 at 07:38
  • Did you compare my query string to yours? They are not the same. Each array key in the query string needs to end with [] . Otherwise each key is interpreted as a key pointing to a string value after the = . And then you just get the last value of the three. So the problem is the request coming in from the website, not PHP or Symfony. – lukas.j Aug 27 '23 at 07:43
  • 1
    Does this answer your question? [How to pass an array within a query string?](https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string) – lukas.j Aug 27 '23 at 07:45
  • Consider using Symfony's generateUrl functionality to see what the url should look like. – Cerad Aug 27 '23 at 14:04

1 Answers1

2

You have to get the QUERY_STRING from the server variables. This has the raw params.

A solution might look like this:

$params = [];
$queryString = $request->server->get('QUERY_STRING');
$paramsQueryString = explode('&', $queryString);
foreach ($paramsQueryString as $paramQueryString) {
    list($key, $value) = explode("=", $paramQueryString);
    if (!isset($params[$key])) {
        $params[$key] = [];
    }
    $params[$key][] = $value;
}

// Which key has only one element? Then remove the array.
foreach ($params as $key => $value) {
    if (count($value) === 1) {
        unset($params[$key]);
        $params[$key] = $value[0];
    }
}

$params

Array
(
    [array] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [property] => aa
)
Bademeister
  • 389
  • 1
  • 10
  • You saved my day. Thanks a lot! @Bademeister – monaxi Aug 28 '23 at 06:50
  • While this does answer the question, the standard in PHP is to use for arrays the syntax with [] in the query string, e.g.: _.../query?array[]=1&array[]=2&array[]=3&property=aa_. In addition most if not all AJAX and 'fetch' libraries use the bracket syntax. And the PHP standard library ( https://www.php.net/manual/en/function.parse-str.php and https://www.php.net/manual/en/function.http-build-query.php) uses query strings with brackets for arrays. And @monaxi's code _$request->query->all()_ would return the correct result had he used the bracket syntax in the query string. – lukas.j Aug 28 '23 at 14:06
  • @lukas.j Yes, it is not a standard. I think that a third party system sends this request and cannot build the parameter structure as PHP requires. – Bademeister Aug 28 '23 at 15:13