Search code examples
phparraysstringquery-stringurl-parsing

Parse a query string into an array


I have a string that looks like:

single=Single&multiple=Multiple2&check=check1&radio=radio2

how could I create a array like this:

array(
  'single' => 'Single',
  'multiple' => 'Multiple2',
  'check' => 'check1',
  'radio' => 'radio2',
)

Solution

  • Use parse_str

    parse_str('single=Single&multiple=Multiple2&check=check1&radio=radio2', $data);
    

    And in $data you will have your variables.