allinurl Posted June 27, 2008 Share Posted June 27, 2008 does anyone knows how to pass an array into a URL, what I mean is if I have Array ( [0] => var1 [1] => var2 ) into the url as parameter, so I end up with something like: url.com?param=$var1¶m=$var2 Thanks in advance Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 I would do this.. my array: <? $vars =array( 'test','ash','din' ); $url ='mypage.php?'; foreach($vars as $param => $value){ $url.="&$param=$value"; } print $url; ?>code] Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2008 Share Posted June 27, 2008 Are you trying to generate an URL from an array? In your example, you're using "param" as a name in the query string twice. That's impossible. If you want to 'convert' an array's keys and values into names and values in a query string, this'll do: <?php $url = 'http://url.com?'; $array = array('name' => 'Joe', 'age' => 23); foreach ($array as $key => $val) { $url .= "$key=$val&"; } $url = substr($url, 0, -1); echo $url; // http://url.com?name=Joe&age=23 ?> Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 Your code does exactly what my example does, except yours uses more lines Quote Link to comment Share on other sites More sharing options...
allinurl Posted June 27, 2008 Author Share Posted June 27, 2008 thanks thats what I was looking for~~ Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2008 Share Posted June 27, 2008 Your code does exactly what my example does, except yours uses more lines Except yours outputs an invalid URL. Hint: placement of ampersand Also, I specified the keys in the array. And included a comment. Who cares anyway. Quote Link to comment Share on other sites More sharing options...
xyn Posted June 27, 2008 Share Posted June 27, 2008 my url wasnt invalid, its not hard to make an associative array :l, and thats the params sorted. but i dont care agreed. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 27, 2008 Share Posted June 27, 2008 Your script outputs "mypage.php?&0=test&1=ash&2=din" which is invalid/malformed. There shouldn't be an ampersand following the question mark. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.