PHP url to array PHP url to array arrays arrays

PHP url to array


PHP has a native function for that, the parse_str function. You can use it like:

parse_str($_SERVER['QUERY_STRING'], $outputArray);

This will do just what you need.


There may be a better way to do this but this is what I came up with.

<?php    $url = 'http://www.domain.com/page?s=194&client=151678&m=a4a&v=1&g=54';    $remove_http = str_replace('http://', '', $url);    $split_url = explode('?', $remove_http);    $get_page_name = explode('/', $split_url[0]);    $page_name = $get_page_name[1];    $split_parameters = explode('&', $split_url[1]);    for($i = 0; $i < count($split_parameters); $i++) {        $final_split = explode('=', $split_parameters[$i]);        $split_complete[$page_name][$final_split[0]] = $final_split[1];    }    var_dump($split_complete);?>

http://codepad.org/xTsAks46


With parse_url() you can parse the URL to an associative array. In the result array you get amongst others the query part. Then you can use parse_str() for parsing the query part to your new array.