Created
March 28, 2024 19:43
-
-
Save petenelson/b3703cc63697e1517ac322a80234bfc0 to your computer and use it in GitHub Desktop.
WordPress Multi Level Parse Args
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
<?php | |
/** | |
* 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