Verification: a143cc29221c9be0

Php array from url string

PHP Function parese_str

PHP has a built in function to perform this task and convert a query string into an array so that you can loop through the parameters and perform the tasks you need to. This PHP function is called parse_str(), it takes two parameters but the second is optional. The first parameter will be a string which will be the query string and the return of the function will create variables from the query string.

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

If you give this a second parameter then the return of this will add query string into an array or key value pairs.

$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

parse_url()

You need to use combination of parse_url() and parse_str().

The parse_url() parse URL and return its components that you can get query string using query key. Then you should use parse_str() that parse query string and return values into variable.

$url = "https://example.com/test/1234?basic=2&email=xyz2@test.com";
parse_str(parse_url($url)['query'], $params);
echo $params['email']; // xyz2@test.com

Also you can do this work using regex.