Skip to content

Instantly share code, notes, and snippets.

@maxyudin
Forked from boonebgorges/gist:5510970
Last active April 19, 2017 16:03
Show Gist options
  • Save maxyudin/5fb700db280cfa6dbb3ff7aea8de7dd7 to your computer and use it in GitHub Desktop.
Save maxyudin/5fb700db280cfa6dbb3ff7aea8de7dd7 to your computer and use it in GitHub Desktop.
A recursive sorta-version of wp_parse_args()
<?php
/**
* Recursive argument parsing
*
* This acts like a multi-dimensional version of wp_parse_args() (minus
* the querystring parsing - you must pass arrays).
*
* Values from $a override those from $b; keys in $b that don't exist
* in $a are passed through.
*
* This is different from array_merge_recursive(), both because of the
* order of preference ($a overrides $b) and because of the fact that
* array_merge_recursive() combines arrays deep in the tree, rather
* than overwriting the b array with the a array.
*
* The implementation of this function is specific to the needs of
* BP_Group_Extension, where we know that arrays will always be
* associative, and that an argument under a given key in one array
* will be matched by a value of identical depth in the other one. The
* function is NOT designed for general use, and will probably result
* in unexpected results when used with data in the wild. See, eg,
* http://core.trac.wordpress.org/ticket/19888
*
* @since BuddyPress (1.8)
* @arg array $a
* @arg array $b
* @return array
*/
public static function parse_args_r( &$a, $b ) {
$a = (array) $a;
$b = (array) $b;
$r = $b;
foreach ( $a as $k => &$v ) {
if ( is_array( $v ) && isset( $r[ $k ] ) ) {
$r[ $k ] = self::parse_args_r( $v, $r[ $k ] );
} else {
$r[ $k ] = $v;
}
}
return $r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment