I have a url which looks like this:
http://localhost/projectcode12may2014/ampanel/index.php?rel=common_listing&module=company&field%5B%5D=address&adv_operation%5B%5D=c&value%5B%5D=sector&query_type%5B%5D=AND&submit=Submit
In the decoded form, it looks like:
http://localhost/projectcode12may2014/ampanel/index.php?rel=common_listing&module=company&field[]=address&adv_operation[]=c&value[]=sector&query_type[]=AND&submit=Submit
I am trying to parse this URL and get the values of field[]
, adv_operation[]
and query_type[]
as arrays, but I am just getting Array
written in text if I try to parse these fields. I am using parse_url()
and parse_str()
for parsing. Can anyone suggest a suitable method for this? Thanks in advance.
You're probably displaying the array section incorrectly, because your method should work.
You use parse_url to extract the "query" segment of the URL, then use parse_str to load it into a variable.
Example:
$url = "http://example.com/page.php?a=apple&b=banana&z=zebra&arr[]=1&arr[]=2";
$query = parse_url($url, PHP_URL_QUERY);
var_dump($query);
// string(40) "a=apple&b=banana&z=zebra&arr[]=1&arr[]=2"
parse_str($query, $parsed);
var_dump($parsed);
/*
array(4) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["z"]=>
string(5) "zebra"
["arr"]=>
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
}
}
*/
// $parsed["arr"] is now array(1, 2);
Below are the output formats of the array parameter:
$url = '<your_url>';
$parsedUrl = parse_url($url, PHP_URL_QUERY);
parse_str($parsedUrl);
echo $field;
/*
outputs as:
Array
*/
echo $field[0];
/*
outputs as:
address
*/
print_r($field);
/*
outputs as:
Array
(
[0] => address
)
*/