WordPress at a glance

map_deep() WP 1.0

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

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

✈ 1 time = 0.000031s = very fast | 50000 times = 0.17s = very fast

No Hooks.

Return

Mixed. The value with the callback applied to all non-arrays and non-objects inside it.

Usage

map_deep( $value, $callback );
$value(mixed) (required)
The array, object, or scalar.
$callback(callable) (required)
The function to map onto $value.

Code of map_deep: wp-includes/formatting.php VER 4.9.8

<?php
function map_deep( $value, $callback ) {
	if ( is_array( $value ) ) {
		foreach ( $value as $index => $item ) {
			$value[ $index ] = map_deep( $item, $callback );
		}
	} elseif ( is_object( $value ) ) {
		$object_vars = get_object_vars( $value );
		foreach ( $object_vars as $property_name => $property_value ) {
			$value->$property_name = map_deep( $property_value, $callback );
		}
	} else {
		$value = call_user_func( $callback, $value );
	}

	return $value;
}

Related Functions

From category: Helper Functions

No comments
    Hello, !     Log In . Register