Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate URL with parameters from an array

Tags:

arrays

php

I need to take an array like I have below:

$subids = Array     (         [s1] => one         [s2] => two         [s3] => three         [s4] => four         [s5] => five         [s6] => six     ) 

and generate a URL such as http://example.com?s1=one&s2=two&s3=three=&s4=four&s5=five&s6=six

All of the subids are not always defined, so sometimes s3 may not be defined, so it shouldn't be appended to the URL. Additionally, whatever the first subid is, it has to have the ? preceding it instead of the ampersand (&)

So if the array is just:

$subids = Array     (         [s2] => two         [s6] => six     ) 

then the URL needs to be http://example.com?s2=two&s6=six

I have the following so far:

$url = 'http://example.com'

    foreach ($subids AS $key => $value) {         $result[$id]['url'] .= '&' . $key . '=' . $value;     } 

However, I'm not sure what the best way would be to append the ? at the beginning of the first key/value pair.

I feel like there's a PHP function to help with this but I'm not finding much. I'm using Codeigniter if there's anything I can use that is provided by CI.

like image 364
Motive Avatar asked Nov 07 '12 18:11

Motive


People also ask

How do you pass an array as a parameter in URL?

You can use http_build_query to generate a URL-encoded querystring from an array in PHP. Whilst the resulting querystring will be expanded, you can decide on a unique separator you want as a parameter to the http_build_query method, so when it comes to decoding, you can check what separator was used.

How do I pass parameters to URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

Can we send array in URL?

We can also use urlencode() function with serialize() function to create an URL-encoded string. In the given example, first we have converted the array into its byte stream representation, which is a string using serialize() function. And then, we have created an URL-encoded string of that byte stream.

Can we pass array in query string Javascript?

We can also pass an array with tuples or a query string. With that done, we now have an instance of the URLSearchParams class. We can get the string version of this by calling toString and append this to our URL.


2 Answers

All you need is http_build_query:

$final = $url . "?" . http_build_query($subids); 
like image 169
Baba Avatar answered Oct 16 '22 11:10

Baba


You can use with http_build_query() function. Example from php.net:

<?php $data = array(     'foo' => 'bar',     'baz' => 'boom',     'cow' => 'milk',     'php' => 'hypertext processor', );  echo http_build_query( $data ) . "\n"; echo http_build_query( $data, '', '&amp;' ); ?> 

And output this lines:

foo=bar&baz=boom&cow=milk&php=hypertext+processor foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor 

You can read from the source: http://www.php.net/manual/en/function.http-build-query.php

BTW, If you use with WordPress, you can this function: http://codex.wordpress.org/Function_Reference/add_query_arg

Have fun. :)

like image 28
Yakir Sitbon Avatar answered Oct 16 '22 09:10

Yakir Sitbon