map_deep() WordPress Function

The map_deep() function applies a user-defined function to each element of an array, recursively. This is useful for applying a function to a multidimensional array. The function is applied to the deepest level of the array first, and works its way up the array. If you need to apply a function to a multidimensional array, map_deep() is the function you need.

map_deep( mixed $value, callable $callback ) #

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


Description

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


Top ↑

Parameters

$value

(mixed)(Required)The array, object, or scalar.

$callback

(callable)(Required)The function to map onto $value.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/formatting.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;
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More