_http_build_query

From php.net (modified by Mark Jaquith to behave like the native PHP5 function).

Description

(string) _http_build_query( (array|object) $data, (constant) $prefix = null, (constant) $sep = null, (string) $key = '', (bool) $urlencode = true ); 

Returns (string)

The query string.

Parameters (5)

0. $data (array|object)
An array or object of data. Converted to array.
1. $prefix — Optional. (constant) => null
Numeric index. If set, start parameter numbering with it. Default null.
2. $sep — Optional. (constant) => null
Argument separator; defaults to arg_separator.output.. Default null.
3. $key — Optional. (string) => ''
Used to prefix key name. Default empty.
4. $urlencode — Optional. (bool) => true
Whether to use urlencode() in the result. Default true.

Usage

  1. if ( !function_exists( '_http_build_query' ) ) { 
  2. require_once ABSPATH . WPINC . '/functions.php'; 
  3.  
  4. // An array or object of data. Converted to array. 
  5. $data = null; 
  6.  
  7. // Optional. Numeric index. If set, start parameter numbering with it. 
  8. // Default null. 
  9. $prefix = null; 
  10.  
  11. // Optional. Argument separator; defaults to 'arg_separator.output'. 
  12. // Default null. 
  13. $sep = null; 
  14.  
  15. // Optional. Used to prefix key name. Default empty. 
  16. $key = ''; 
  17.  
  18. // Optional. Whether to use urlencode() in the result. Default true. 
  19. $urlencode = true; 
  20.  
  21. // NOTICE! Understand what this does before running. 
  22. $result = _http_build_query($data, $prefix, $sep, $key, $urlencode); 
  23.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/functions.php  
  1. function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) { 
  2. $ret = array(); 
  3.  
  4. foreach ( (array) $data as $k => $v ) { 
  5. if ( $urlencode) 
  6. $k = urlencode($k); 
  7. if ( is_int($k) && $prefix != null ) 
  8. $k = $prefix.$k; 
  9. if ( !empty($key) ) 
  10. $k = $key . '%5B' . $k . '%5D'; 
  11. if ( $v === null ) 
  12. continue; 
  13. elseif ( $v === false ) 
  14. $v = '0'; 
  15.  
  16. if ( is_array($v) || is_object($v) ) 
  17. array_push($ret, _http_build_query($v, '', $sep, $k, $urlencode)); 
  18. elseif ( $urlencode ) 
  19. array_push($ret, $k.'='.urlencode($v)); 
  20. else 
  21. array_push($ret, $k.'='.$v); 
  22.  
  23. if ( null === $sep ) 
  24. $sep = ini_get('arg_separator.output'); 
  25.  
  26. return implode($sep, $ret);