wp_json_encode

函式
wp_json_encode ( $data, $options = 0, $depth = 512 )
引數
  • (mixed) $data Variable (usually an array or object) to encode as JSON.
    Required:
  • (int) $options Optional. Options to be passed to json_encode(). Default 0.
    Required:
  • (int) $depth Optional. Maximum depth to walk through $data. Must be greater than 0. Default 512.
    Required:
    Default: 512
返回值
  • (string|false) The JSON encoded string, or false if it cannot be encoded.
定義位置
相關方法
wp_json_file_decodewp_send_json_errorwp_is_json_media_typewp_is_json_request_wp_iso_convert
引入
4.1.0
棄用
-

wp_json_encode: 這個函式用來將一個PHP變數編碼為JSON字串。它接收一個PHP變數作為引數,並返回一個JSON編碼的字串。

將一個變數編碼為JSON,並進行一些合理性檢查。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
$json = json_encode( $data, $options, $depth );
// If json_encode() was successful, no need to do more sanity checking.
if ( false !== $json ) {
return $json;
}
try {
$data = _wp_json_sanity_check( $data, $depth );
} catch ( Exception $e ) {
return false;
}
return json_encode( $data, $options, $depth );
}
function wp_json_encode( $data, $options = 0, $depth = 512 ) { $json = json_encode( $data, $options, $depth ); // If json_encode() was successful, no need to do more sanity checking. if ( false !== $json ) { return $json; } try { $data = _wp_json_sanity_check( $data, $depth ); } catch ( Exception $e ) { return false; } return json_encode( $data, $options, $depth ); }
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
	$json = json_encode( $data, $options, $depth );

	// If json_encode() was successful, no need to do more sanity checking.
	if ( false !== $json ) {
		return $json;
	}

	try {
		$data = _wp_json_sanity_check( $data, $depth );
	} catch ( Exception $e ) {
		return false;
	}

	return json_encode( $data, $options, $depth );
}

常見問題

FAQs
檢視更多 >