_http_build_query

函式
_http_build_query ( $data, $prefix = null, $sep = null, $key = '', $urlencode = true )
Access
Private
引數
  • (array|object) $data An array or object of data. Converted to array.
    Required:
  • (string) $prefix Optional. Numeric index. If set, start parameter numbering with it. Default null.
    Required:
    Default: null
  • (string) $sep Optional. Argument separator; defaults to 'arg_separator.output'. Default null.
    Required:
    Default: null
  • (string) $key Optional. Used to prefix key name. Default empty.
    Required:
    Default: (empty)
  • (bool) $urlencode Optional. Whether to use urlencode() in the result. Default true.
    Required:
    Default: true
返回值
  • (string) The query string.
相關
  • https://www.php.net/manual/en/function.http-build-query.php
定義位置
相關方法
build_query_wp_filter_build_unique_idwp_http_validate_url_wp_upload_dirwp_reset_query
引入
3.2.0
棄用
-

http_build_query是一個PHP函式,在WordPress中也可用。它接收一個資料陣列,並從中建立一個URL編碼的查詢字串,適合在URL中使用: 這個函式有幾個選項,可以用來控制編碼的行為,例如指定欄位之間的分隔符,控制是否包括空值的欄位,等等。在WordPress中,這個函式通常用於為外部API的請求建立URL,或者用於在網站內構建連結。

來自php.net(由Mark Jaquith修改,使其行為類似於本地的PHP5函式)。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
$ret = array();
foreach ( (array) $data as $k => $v ) {
if ( $urlencode ) {
$k = urlencode( $k );
}
if ( is_int( $k ) && null != $prefix ) {
$k = $prefix . $k;
}
if ( ! empty( $key ) ) {
$k = $key . '%5B' . $k . '%5D';
}
if ( null === $v ) {
continue;
} elseif ( false === $v ) {
$v = '0';
}
if ( is_array( $v ) || is_object( $v ) ) {
array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) );
} elseif ( $urlencode ) {
array_push( $ret, $k . '=' . urlencode( $v ) );
} else {
array_push( $ret, $k . '=' . $v );
}
}
if ( null === $sep ) {
$sep = ini_get( 'arg_separator.output' );
}
return implode( $sep, $ret );
}
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) { $ret = array(); foreach ( (array) $data as $k => $v ) { if ( $urlencode ) { $k = urlencode( $k ); } if ( is_int( $k ) && null != $prefix ) { $k = $prefix . $k; } if ( ! empty( $key ) ) { $k = $key . '%5B' . $k . '%5D'; } if ( null === $v ) { continue; } elseif ( false === $v ) { $v = '0'; } if ( is_array( $v ) || is_object( $v ) ) { array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) ); } elseif ( $urlencode ) { array_push( $ret, $k . '=' . urlencode( $v ) ); } else { array_push( $ret, $k . '=' . $v ); } } if ( null === $sep ) { $sep = ini_get( 'arg_separator.output' ); } return implode( $sep, $ret ); }
function _http_build_query( $data, $prefix = null, $sep = null, $key = '', $urlencode = true ) {
	$ret = array();

	foreach ( (array) $data as $k => $v ) {
		if ( $urlencode ) {
			$k = urlencode( $k );
		}
		if ( is_int( $k ) && null != $prefix ) {
			$k = $prefix . $k;
		}
		if ( ! empty( $key ) ) {
			$k = $key . '%5B' . $k . '%5D';
		}
		if ( null === $v ) {
			continue;
		} elseif ( false === $v ) {
			$v = '0';
		}

		if ( is_array( $v ) || is_object( $v ) ) {
			array_push( $ret, _http_build_query( $v, '', $sep, $k, $urlencode ) );
		} elseif ( $urlencode ) {
			array_push( $ret, $k . '=' . urlencode( $v ) );
		} else {
			array_push( $ret, $k . '=' . $v );
		}
	}

	if ( null === $sep ) {
		$sep = ini_get( 'arg_separator.output' );
	}

	return implode( $sep, $ret );
}

常見問題

FAQs
檢視更多 >