map_deep( mixed $value, callable $callback )

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


Description Description

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


Parameters Parameters

$value

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

$callback

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


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/formatting.php

4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
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

Changelog
Version Description
4.4.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.