note 75505 added to function.parse-str
| From: | mike dot coley at inbox dot com | Date: | Sat, 02 Jun 2007 19:25:01 +0000 |
| Subject: | note 75505 added to function.parse-str | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127128@lists.php.net to get a copy of this message | ||
Here is a little function that does the opposite of the parse_str function. It will take an array
and build a query string from it.
<?php
/* Converts an array of parameters into a query string to be appended to a URL.
*
* @return string : Query string to append to a URL.
* @param array $array : Array of parameters to append to the query string.
* @param string $parent : This should be left blank (it is used internally by the function).
*/
function append_params($array, $parent='')
{
$params = array();
foreach ($array as $k => $v)
{
if (is_array($v))
$params[] = append_params($v, (empty($parent) ? urlencode($k) : $parent . '[' .
urlencode($k) . ']'));
else
$params[] = (!empty($parent) ? $parent . '[' . urlencode($k) . ']' :
urlencode($k)) . '=' . urlencode($v);
}
$sessid = session_id();
if (!empty($parent) || empty($sessid))
return implode('&', $params);
// Append the session ID to the query string if we have to.
$sessname = session_name();
if (ini_get('session.use_cookies'))
{
if (!ini_get('session.use_only_cookies') && (!isset($_COOKIE[$sessname]) ||
($_COOKIE[$sessname] != $sessid)))
$params[] = $sessname . '=' . urlencode($sessid);
}
elseif (!ini_get('session.use_only_cookies'))
$params[] = $sessname . '=' . urlencode($sessid);
return implode('&', $params);
}
?>
Note that the function will also append the session ID to the query string if it needs to be.
----
Server IP: 87.124.64.96
Probable Submitter: 82.37.81.134
----
Manual Page -- http://www.php.net/manual/en/function.parse-str.php
Edit -- https://master.php.net/note/edit/75505
Del: integrated -- https://master.php.net/note/delete/75505/integrated
Del: useless -- https://master.php.net/note/delete/75505/useless
Del: bad code -- https://master.php.net/note/delete/75505/bad+code
Del: spam -- https://master.php.net/note/delete/75505/spam
Del: non-english -- https://master.php.net/note/delete/75505/non-english
Del: in docs -- https://master.php.net/note/delete/75505/in+docs
Del: other reasons-- https://master.php.net/note/delete/75505
Reject -- https://master.php.net/note/reject/75505
Search -- https://master.php.net/manage/user-notes.php