1

I don't know how to make this working. I want to make arrays from URL:

index.php?address=someaddress&telephone=12345&customer=Customer Name&p_name[0]=ProductOne&p_name[1]=ProductTwo&p_price[0]=1&p_price[1]=10&p_name[2]...

There is an api, which is working like this:

$api‐>addItem(array(
        'name' => 'ProductOne',
        'price' => '123',
    ));

$api­‐>addItem(array(
        'name' => 'ProductTwo',
        'price' => '32',
    ));

Is there any way to make arrays like this (=api request $api->addItem(array) from the URL? $api­‐>addItem(array can be used multiple times.

2
  • Are there really $ in the URL? Are those supposed to be &? Commented May 11, 2012 at 19:53
  • Is this URL a string, or are you going to this URL? Commented May 11, 2012 at 19:56

6 Answers 6

3

EDIT: Thanks dqhendricks and Rocket for pointing out that you can use parse_str() to do the same thing.

$q = parse_str(parse_url($url, PHP_URL_QUERY));

Or you could use this (the long way):

function parse_query($var) {
   $var = parse_url($var, PHP_URL_QUERY);
   $var = html_entity_decode($var);
   $var = explode('&', $var);
   $arr = array();

   foreach($var as $val) {
     $x = explode('=', $val);
     $arr[$x[0]] = $x[1];
   }
   unset($val, $x, $var);
   return $arr;
}

Use like this:

$url = "http://someurl.com/click?z=26&a=761";
print_r(parse_query($url));
Sign up to request clarification or add additional context in comments.

2 Comments

i cannot believe this got this many votes. there is already native php code to do this for you... parse_str()
$q = parse_str(parse_url($url, PHP_URL_QUERY));
2

Do you have control over the URL? If so, I'd change how you send your values.

Instead of:

name1=ProductOne&price1=123&name2=ProductTwo&price2=32

I'd use:

name[]=ProductOne&price[]=123&name[]=ProductTwo&price[]=32

The [] turn them into arrays, meaning $_GET['name'] is now an array. then you can foreach over it.

foreach($_GET['name'] as $k=>$v){
    $api->addItem(array(
        'name' => $v,
        'price' => $_GET['price'][$k]
    ));
}

17 Comments

Yes, I have full control over the URL. I think your code is what I exactly need, I am going to try it.
hmm, I can't make it working bit.ly/KuQ4H8. So the only way is parse_str&parse_url? The problem with that, there will be additional elements in the URL, which will be not in this array. I update the question.
@Adrian: What's the problem you're having?
The array in the URL must be p_name[0], p_name[1]... I can't make array like this p_name[]. index.php?address=someaddress&telephone=12345&customer=Customer Name&p_name[0]=ProductOne&p_name[1]=ProductTwo&p_quantity[0]=1&p_quantity[1]=1
@Adrian: ...What's the problem? You can put values inside the [], but you don't have to. It'll work either way.
|
1
// extract the query from the url string
$url = parse_url('sample.php?name1=ProductOne&price1=123&name2=ProductTwo&price2=32', PHP_URL_QUERY);
// process into array so that first element is a key, and second element is a value
parse_str($url, $output_array);
// now $output_array contains the query's variables.

The bigger question is why would you want to do this when these variables are already contained in $_GET?

Comments

0
sample.php?name[]=Product1&name[]=Product2

Then in your PHP, you can see:

print_r($_GET['name']);

Comments

0

I'm not sure if I understood you right, but if what you're looking for is converting a querystring to an array you can user the pares_str function. in order to get only the querystring from a url you can use the parse_url function

$url = "sample.php?some_var=someval&s=4&bla=bla";
$url_parts = parse_url($url);
$qs = $url_parts["query"];
$params = array();
parse_str($qs,$params);
var_dump($params);

Comments

0

Like dqhendricks suggested, would it not pay to just use the raw gets and then append them to an array. I am assuming that you will always know what will be in the URL

$array_of_gets = array("address" => $_GET['address'],"telephone" => $_GET['telephone']);

and so on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.