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.
Parameters
$value
mixedrequired- The array, object, or scalar.
$callback
callablerequired- The function to map onto $value.
Source
*
* This is similar to `array_walk_recursive()` but acts upon objects too.
*
* @since 4.4.0
*
* @param mixed $value The array, object, or scalar.
* @param callable $callback The function to map onto $value.
* @return mixed The value with the callback applied to all non-arrays and non-objects inside it.
*/
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 );
Changelog
Version | Description |
---|---|
4.4.0 | Introduced. |
You can use
map_deep
to easily sanitize user input.This works the same for multidimensional arrays.