| // | Aidan Lister // +----------------------------------------------------------------------+ // // $Id: http_build_query.php,v 1.1 2004-10-26 18:22:16 kostja Exp $ // /** * Replace function http_build_query() * * @category PHP * @package PHP_Compat * @link http://php.net/function.http-build-query * @author Stephan Schmidt * @author Aidan Lister * @version $Revision: 1.1 $ * @since PHP 5 * @require PHP 4.0.1 (trigger_error) */ if (!function_exists('http_build_query')) { function http_build_query ($formdata, $numeric_prefix = null) { // If $formdata is an object, convert it to an array if (is_object($formdata)) { $formdata = get_object_vars($formdata); } // Check we have an array to work with if (!is_array($formdata)) { trigger_error('http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given.', E_USER_WARNING); return false; } // If the array is empty, return null if (empty($formdata)) { return null; } // Start building the query $tmp = array (); foreach ($formdata as $key => $val) { if (is_integer($key) && $numeric_prefix != null) { $key = $numeric_prefix . $key; } if (is_scalar($val)) { array_push($tmp, urlencode($key).'='.urlencode($val)); continue; } // If the value is an array, recursively parse it if (is_array($val)) { array_push($tmp, __http_build_query($val, urlencode($key))); continue; } } return implode('&', $tmp); } // Helper function function __http_build_query ($array, $name) { $tmp = array (); foreach ($array as $key => $value) { if (is_array($value)) { array_push($tmp, __http_build_query($value, sprintf('%s[%s]', $name, $key))); } elseif (is_scalar($value)) { array_push($tmp, sprintf('%s[%s]=%s', $name, urlencode($key), urlencode($value))); } elseif (is_object($value)) { array_push($tmp, __http_build_query(get_object_vars($value), sprintf('%s[%s]', $name, $key))); } } return implode('&', $tmp); } } ?>