PHP convert a commercial sentence (&) to array

0

It has a native PHP function that converts a QueryString into Array

Example: page=index&produto=115&usuario=2

Array(
[0] => 'page=index',
[1] => 'produto=115',
[2] => 'usuario=2'
)

I know I can use explode('&' QueryString) , but remember I had a function that already identified & as a separator for the array.

Thank you.

    
asked by anonymous 13.06.2016 / 10:31

1 answer

1

As @Sergio said, PHP does this automatically if it is a URL. If this is a string, you can use the parse_str function.

  

parse_str ( string $str [, array &$arr ] ) Converts str as if it had been passed via URL and sets the value of the variables.

$str = "first=value&arr[]=foo+bar&arr[]=baz";

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz
    
13.06.2016 / 19:48