array_replace_recursive

PHP-agnostic version of {@link array_replace_recursive()}.

Description

(array) array_replace_recursive( (array) $base = array(), (array) $replacements = array() ); 

The array_replace_recursive() function is a PHP 5.3 function. WordPress currently supports down to PHP 5.2, so this method is a workaround for PHP 5.2.

Note: array_replace_recursive() supports infinite arguments, but for our use- case, we only need to support two arguments.

Subject to removal once WordPress makes PHP 5.3.0 the minimum requirement.

Returns (array)

Parameters (2)

0. $base — Optional. (array) => array()
Array with keys needing to be replaced.
1. $replacements — Optional. (array) => array()
Array with the replaced keys.

Usage

  1. if ( !function_exists( 'array_replace_recursive' ) ) { 
  2. require_once ABSPATH . '/wp-admin/nav-menus.php'; 
  3.  
  4. // Array with keys needing to be replaced. 
  5. $base = array(); 
  6.  
  7. // Array with the replaced keys. 
  8. $replacements = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = array_replace_recursive($base, $replacements); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/nav-menus.php  
  1. function array_replace_recursive( $base = array(), $replacements = array() ) { 
  2. foreach ( array_slice( func_get_args(), 1 ) as $replacements ) { 
  3. $bref_stack = array( &$base ); 
  4. $head_stack = array( $replacements ); 
  5.  
  6. do { 
  7. end( $bref_stack ); 
  8.  
  9. $bref = &$bref_stack[ key( $bref_stack ) ]; 
  10. $head = array_pop( $head_stack ); 
  11.  
  12. unset( $bref_stack[ key( $bref_stack ) ] ); 
  13.  
  14. foreach ( array_keys( $head ) as $key ) { 
  15. if ( isset( $key, $bref ) && 
  16. isset( $bref[ $key ] ) && is_array( $bref[ $key ] ) && 
  17. isset( $head[ $key ] ) && is_array( $head[ $key ] ) 
  18. ) { 
  19. $bref_stack[] = &$bref[ $key ]; 
  20. $head_stack[] = $head[ $key ]; 
  21. } else { 
  22. $bref[ $key ] = $head[ $key ]; 
  23. } while ( count( $head_stack ) ); 
  24.  
  25. return $base;