[12.x] Add recursive map helper #57739
Closed
+145
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
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.