wpseek.com
Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_parse_args › WordPress Function

Depuis2.2.0
Dépréciéen/a
› wp_parse_args ( $args, $defaults = array() )
Paramètres: (2)
  • (string|array<string,mixed>|object) $args Value to merge with $defaults.
    Requis: Oui
  • (array<string,mixed>) $defaults Optional. Array that serves as the defaults. Default empty array.
    Requis: Non
    Défaut: array()
Retourne:
  • (array<string,mixed>) Merged user defined values with defaults.
Défini(e) dans:
Codex:
Changelog:
  • 2.3.0

Merges user defined arguments into defaults array.

This function is used throughout WordPress to allow for both string or array to be merged into another array. The keys of the returned array are documented as strings, since that is what callers mean by them, but they are not promised as such to static analysis. Integer keys remain reachable through arguments that are perfectly valid: json_decode( '{"0":"a"}' ) is an object whose properties get_object_vars() reports under an integer key, and parse_str() reads 0=a as one as well.


Source

function wp_parse_args( $args, $defaults = array() ): array {
	if ( is_object( $args ) ) {
		$parsed_args = get_object_vars( $args );
	} elseif ( is_array( $args ) ) {
		$parsed_args =& $args;
	} else {
		wp_parse_str( $args, $parsed_args );
	}

	if ( is_array( $defaults ) && $defaults ) {
		return array_merge( $defaults, $parsed_args );
	}
	return $parsed_args;
}