| | 1669 | |
| | 1670 | /** |
| | 1671 | * PHP-agnostic version of {@link array_replace_recursive()}. |
| | 1672 | * |
| | 1673 | * array_replace_recursive() is a PHP 5.3 function. BuddyPress (and |
| | 1674 | * WordPress) currently supports down to PHP 5.2, so this method is a workaround |
| | 1675 | * for PHP 5.2. |
| | 1676 | * |
| | 1677 | * Note: array_replace_recursive() supports infinite arguments, but for our use- |
| | 1678 | * case, we only need to support two arguments. |
| | 1679 | * |
| | 1680 | * Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement. |
| | 1681 | * |
| | 1682 | * @since BuddyPress (2.2.0) |
| | 1683 | * |
| | 1684 | * @see http://php.net/manual/en/function.array-replace-recursive.php#109390 |
| | 1685 | * |
| | 1686 | * @param array $base Array with keys needing to be replaced |
| | 1687 | * @param array $replacements Array with the replaced keys |
| | 1688 | * @return array |
| | 1689 | */ |
| | 1690 | protected static function array_replace_recursive( $base = array(), $replacements = array() ) { |
| | 1691 | if ( function_exists( 'array_replace_recursive' ) ) { |
| | 1692 | return array_replace_recursive( $base, $replacements ); |
| | 1693 | } |
| | 1694 | |
| | 1695 | // PHP 5.2-compatible version |
| | 1696 | // http://php.net/manual/en/function.array-replace-recursive.php#109390 |
| | 1697 | foreach ( array_slice( func_get_args(), 1 ) as $replacements ) { |
| | 1698 | $bref_stack = array( &$base ); |
| | 1699 | $head_stack = array( $replacements ); |
| | 1700 | |
| | 1701 | do { |
| | 1702 | end( $bref_stack ); |
| | 1703 | |
| | 1704 | $bref = &$bref_stack[ key( $bref_stack ) ]; |
| | 1705 | $head = array_pop( $head_stack ); |
| | 1706 | |
| | 1707 | unset( $bref_stack[ key($bref_stack) ] ); |
| | 1708 | |
| | 1709 | foreach ( array_keys( $head ) as $key ) { |
| | 1710 | if ( isset( $key, $bref ) && is_array( $bref[$key] ) && is_array( $head[$key] ) ) { |
| | 1711 | $bref_stack[] = &$bref[ $key ]; |
| | 1712 | $head_stack[] = $head[ $key ]; |
| | 1713 | } else { |
| | 1714 | $bref[ $key ] = $head[ $key ]; |
| | 1715 | } |
| | 1716 | } |
| | 1717 | } while( count( $head_stack ) ); |
| | 1718 | } |
| | 1719 | |
| | 1720 | return $base; |
| | 1721 | } |