map_deep

Maps a function to all non-iterable elements of an array or an object.

Description

map_deep( (mixed) $value, (callable) $callback ); 

This is similar to array_walk_recursive() but acts upon objects too.

Parameters (2)

0. $value (mixed)
The array, object, or scalar.
1. $callback (callable)
The function to map onto $value.

Usage

  1. if ( !function_exists( 'map_deep' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // The array, object, or scalar. 
  5. $value = null; 
  6.  
  7. // The function to map onto $value. 
  8. $callback = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = map_deep($value, $callback); 
  12.  

Defined (1)

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

/wp-includes/formatting.php  
  1. function map_deep( $value, $callback ) { 
  2. if ( is_array( $value ) ) { 
  3. foreach ( $value as $index => $item ) { 
  4. $value[ $index ] = map_deep( $item, $callback ); 
  5. } elseif ( is_object( $value ) ) { 
  6. $object_vars = get_object_vars( $value ); 
  7. foreach ( $object_vars as $property_name => $property_value ) { 
  8. $value->$property_name = map_deep( $property_value, $callback ); 
  9. } else { 
  10. $value = call_user_func( $callback, $value ); 
  11.  
  12. return $value;