Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created March 28, 2024 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petenelson/b3703cc63697e1517ac322a80234bfc0 to your computer and use it in GitHub Desktop.
Save petenelson/b3703cc63697e1517ac322a80234bfc0 to your computer and use it in GitHub Desktop.
WordPress Multi Level Parse Args
<?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