2

When we make GET request using query string like this:

answers[0][answer]=ABC2&answers[0][id]=1&answers[1][answer]=XYZ&answers[1][id]=2&answers[2][answer]=QWE&answers[2][id]=3

PHP parses this string to an associative like following

Array
(
    [answers] => Array
        (
            [0] => Array
                (
                    [answer] => ABC2
                    [id] => 1
                )

            [1] => Array
                (
                    [answer] => XYZ
                    [id] => 2
                )

            [2] => Array
                (
                    [answer] => QWE
                    [id] => 3
                )

        )

)

How can I do the same manually with a string stored in a variable?

Amarjit Singh
  • 2,068
  • 19
  • 52
  • 1
    Have you tried [`parse_str`](https://www.php.net/manual/en/function.parse-str.php) – Dharman Jun 04 '19 at 21:59
  • [serialize](https://www.php.net/manual/en/function.serialize.php) / [unserialize](https://www.php.net/manual/en/function.unserialize.php) or [json-encode](https://www.php.net/manual/en/function.json-encode.php) / [json-decode](https://www.php.net/manual/en/function.json-decode.php) could be options as well depending on what you're trying to do. – khartnett Jun 04 '19 at 23:03

1 Answers1

2

Thanks to Dharman

parse_str() worked as I wanted. However parse_str() does not return the parsed variables. It just creates the variable in current scope.

Amarjit Singh
  • 2,068
  • 19
  • 52