I have a url with parameters appended to it. I want to know if there's a way (similar to explode) that seperates a string based on a delimiter and makes the string on the left of the delimiter the key of an array and the string on the right of the delimiter the value.

Example:
I have the following url - http://www.domain.com?param1=value1&param2=value2

I remove the domain name and the question mark to leave me with the parameters. I then explode the parameters using the delimiter '&' so that I have an array with two entries as follows:

$params = array('param1=value1', 'param2=value2');

I want to create a new array from this so that I end up with an array like the following:

$param_values = array(
    'param1' => 'value1',
    'param2' => 'value2'
);

I know how to go about this manually. I'm just wondering if there is a php function that does the same?

Member Avatar for diafol

Soemthing like?

$get = $_GET;
$r[$get['param1']] = $get['param2'];

Obviously you'd have to check the existence and validity of the data before doing this.

Hey diafol

That's not exactly what I'm looking for but the concept is similar to how I'd go about doing it manually except I'd do it like this:

$params = array('param1=value1', 'param2=value2')

foreach($params as $items)
{
    $item = explode('=', $items);

    $param_values[$item[0]] = $item[1];
}

I'm looking for a native php function that does this, but if there isn't one I'll just stick to the tried and tested.

Member Avatar for diafol

Can't say that I've seen a native function for this. Doesn't mean to say that one doesn't exist though.

After some searching I wasn't able to find one. I guess my manual solution will have to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.