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 Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/ 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] Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-575973 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 ?> Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-575980 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 Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-575985 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~~ Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-575995 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. Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-576000 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. Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-576005 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. Link to comment https://forums.phpfreaks.com/topic/112204-passing-array-as-param/#findComment-576067 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.