Last active
May 5, 2022 17:12
-
-
Save petenelson/0bacab0052e1a4683a48ae296252fac5 to your computer and use it in GitHub Desktop.
WordPress: wp_parse_args() with multidimensional array support.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* wp_parse_args() with support for multi-level arrays. | |
* | |
* @param array &$a Arrays to be parsed | |
* @param array $b Defaults for the arrays. | |
* @return array | |
*/ | |
function multi_level_wp_parse_args( &$a, $b ) { | |
// https://mekshq.com/recursive-wp-parse-args-wordpress-function/ | |
// Similar to wp_parse_args() just a bit extended to work with multidimensional arrays :) | |
$a = (array) $a; | |
$b = (array) $b; | |
$result = $b; | |
foreach ( $a as $k => &$v ) { | |
if ( is_array( $v ) && isset( $result[ $k ] ) ) { | |
$result[ $k ] = multi_level_wp_parse_args( $v, $result[ $k ] ); | |
} else { | |
$result[ $k ] = $v; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment