Skip to content

Conversation

@Ashot1995
Copy link
Contributor

This pull request introduces a new static method, mapRecursive, to extend Laravel’s array utilities. The method enables applying a callback function recursively across deeply nested data structures such as arrays, Arrayable, Enumerable, Traversable, Jsonable, and JsonSerializable objects.

Key Features

Recursive mapping: Handles multi-level data structures without manual flattening.
Broad compatibility: Supports Laravel’s common collection and arrayable interfaces, as well as native traversable types.
Flexible callback: Gracefully handles callbacks that accept either one or two parameters (value and key).
Optional key preservation: Allows preserving original keys or reindexing results.

Example Usage

$result = Arr::mapRecursive($data, function ($value, $key) {
    return is_string($value) ? Str::upper($value) : $value;
});

Benefits
Simplifies recursive transformations without additional loops.
Improves code readability for deeply nested array processing.
Extends Laravel’s array manipulation capabilities in a backward-compatible way.

@shaedrich
Copy link
Contributor

shaedrich commented Nov 11, 2025

Theoretically, one could alternatively alter the existing Arr::map() method to allow it to optionally be used recursively:

    /**
     * Run a map over each of the items in the array.
     *
     * @param  array  $array
     * @param  callable  $callback
     * @return array
     */
    public static function map(array $array, callable $callback)
    {
        $keys = array_keys($array);

        try {
-           $items = array_map($callback, $array, $keys);
+           $items = array_map(fn ($value, $key) => $callback($value, $key, fn (array $array, ? callable $fn = null) => self::map($array, $fn ?? $callback)), $array, $keys);
        } catch (ArgumentCountError) {
-           $items = array_map($callback, $array);
+           $items = array_map(fn ($value) => $callback($value, fn (array $array, ? callable $fn = null) => self::map($array, $fn ?? $callback)), $array);
        }

        return array_combine($keys, $items);
    }
Arr::map(
    ['foo' => 'bar', 'baz' => ['lorem' => 'ipsum'],
    fn ($value, $key, $fn) => is_scalar($value)
        ? $key . ': ' . $value
        : $key . ': ' . $fn($value),
);

Another alternative would be to introduce a Arr::flatMap() helper but this would be more restrictive. Attempts to add such a method, though, have been failed once in #32431, while the Collection class already has it.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants