I have an array from a $_GET say
Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
for which i am using this while loop to create a string:
while (list($key, $value) = each($_GET)) {
$get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
}
Now if i get an array from $_GET say like:
Array
(
[0] => pid
[1] => gid
[2] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
)
then in this case what could be the possible changes be done to while loop so that I can avoid the result like this http://www.example.com/shopping_cart.php?0=pid&1=gid&2=Array in url when i use this to redirect it.
I want the url to display the values properly..not like “2=Array“.. how can i do this?
EDIT
Thanks Folks for the help, but I cannot introduce new function neither I can replace the while loop with for loop, I would be very thankfull if you can help me in re-editing the given WHILE Loop…
EDIT 2
I am using header(location:$get_url)
for redirecting to created url, is this the problem of display of “2=Array” in url?
EDIT 3
functions used to build query, NOTE: THESE FUNCTIONS ARE INBUILT FUNCTION OF osCommerce
I still changed one of it by introducing the foreach loop into it see below the use and function definition:
function tep_redirect($url) {
if ( (strstr($url, "n") != false) || (strstr($url, "r") != false) ) {
tep_redirect(tep_href_link(FILENAME_DEFAULT, '', 'NONSSL', false));
}
if ( (ENABLE_SSL == true) && (getenv('HTTPS') == 'on') ) { // We are loading an SSL page
if (substr($url, 0, strlen(HTTP_SERVER . DIR_WS_HTTP_CATALOG)) == HTTP_SERVER . DIR_WS_HTTP_CATALOG) { // NONSSL url
$url = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . substr($url, strlen(HTTP_SERVER . DIR_WS_HTTP_CATALOG)); // Change it to SSL
}
}
$url = str_replace("&", "&", $url);
header('Location: ' . $url);
tep_exit();
}
=========================
function tep_href_link($page = '', $parameters = '', $connection = 'NONSSL', $add_session_id = true, $search_engine_safe = true) {
global $request_type, $session_started, $SID, $spider_flag;
if (!tep_not_null($page)) {
die('</td></tr></table></td></tr></table><br><br><font color="#ff0000">' . TEP_HREF_LINK_ERROR1);
}
if ($connection == 'NONSSL') {
$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
} elseif ($connection == 'SSL') {
if (ENABLE_SSL == true) {
$link = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG;
} else {
$link = HTTP_SERVER . DIR_WS_HTTP_CATALOG;
}
} else {
die('</td></tr></table></td></tr></table><br><br><font color="#ff0000">' . TEP_HREF_LINK_ERROR2);
}
if (tep_not_null($parameters)) {
while ( (substr($parameters, -5) == '&') ) $parameters = substr($parameters, 0, strlen($parameters)-5);
$link .= $page . '?' . tep_output_string($parameters);
$separator = '&';
} else {
$link .= $page;
$separator = '?';
}
// if session is not started or requested not to add session, skip it
if ( ($add_session_id == true) && ($session_started == true) ){
// if cookies are not set and not forced, then add the session info incase the set cookie fails
if ( ! isset($_COOKIE[tep_session_name()]) && (SESSION_FORCE_COOKIE_USE == 'False') ) {
$_sid = tep_session_name() . '=' . tep_session_id();
// if we are chaning modes and cookie domains differ, we need to add the session info
} elseif ( HTTP_COOKIE_DOMAIN . HTTP_COOKIE_PATH != HTTPS_COOKIE_DOMAIN . HTTPS_COOKIE_PATH
&&
(
( $request_type == 'NONSSL' && $connection == 'SSL' && ENABLE_SSL == true )
||
( $request_type == 'SSL' && $connection == 'NONSSL' )
)
) {
$_sid = tep_session_name() . '=' . tep_session_id();
}
}
if (isset($_sid) && !$spider_flag) {
$link .= $separator . tep_output_string($_sid);
}
return $link;
}
===========================
function tep_get_all_get_paramtrs($exclude_array = '') {
global $HTTP_GET_VARS;
if (!is_array($exclude_array)) $exclude_array = array();
$get_url = '';
if (is_array($HTTP_GET_VARS) && (sizeof($HTTP_GET_VARS) > 0))
{
reset($HTTP_GET_VARS);
foreach($HTTP_GET_VARS as $key => $a)
{
if(is_array($a))
{
foreach($a as $k => $v)
{
$get_url[] = $key . '[]' . '=' . rawurlencode(stripslashes($v));
}
}
else
{
$get_url[] = $key . '=' . rawurlencode(stripslashes($a));
}
}
/* while (list($key, $value) = each($HTTP_GET_VARS))
{
if(!is_array($value))
{
if ( (strlen($value) > 0) && ($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') )
{
$get_url .= $key . '=' . rawurlencode(stripslashes($value));
}
}
else
{
if ( (strlen($value) > 0) && ($key != tep_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array)) && ($key != 'x') && ($key != 'y') )
{
$get_url .= preg_replace('/#d/','[]',http_build_query($value,$key.'#'));
}
/* while(list($key1, $value1) = each($value))
{
if ( (strlen($value1) > 0) && ($key1 != tep_session_name()) && ($key1 != 'error') && (!in_array($key1, $exclude_array)) && ($key1 != 'x') && ($key1 != 'y') )
{
$get_url .= $key1 . '=' . rawurlencode(stripslashes($value1));
}
}*/
/* }
}*/
$get_url .= '&';
}
return $get_url;
}
========================
tep_redirect(tep_href_link($goto, tep_get_all_get_paramtrs($parameters)));
here $parameters is an array with two values which doesnt have any resemblence with url display logic
6
Answers
If you want to create an url from a multi-dimensional array, you should use a recursion, or just the built-in php function, which results the same as the function I created http-build-query() (just as Maurice Kherlakian said). It’s the easiest way for doing this.
A recursive function example:
You could check with
is_array
and useimplode
or just loop through the array if you want different keys such as 2_0, 2_1.You can check if the value is an array, then recurse or iterate over it…
http://php.net/manual/en/function.is-array.php
You could use serialize to serialize the array into a single string and urlencode that resulting string, on the receiving end you would then use urldecode and unserialize.
did you consider using http_build_query() or http_build_url()?
header() has nothing to do with your problem.
you just fail to build proper query string
And I wonder why your problem is still persists despite of all these http_build_query() you’ve been told already