Skip to content

[5.8] Add replace() and replaceRecursive() methods on collections#29088

Merged
taylorotwell merged 2 commits intolaravel:5.8from
iamgergo:5.8
Jul 6, 2019
Merged

[5.8] Add replace() and replaceRecursive() methods on collections#29088
taylorotwell merged 2 commits intolaravel:5.8from
iamgergo:5.8

Conversation

@iamgergo
Copy link
Copy Markdown
Contributor

@iamgergo iamgergo commented Jul 5, 2019

This PR implements the PHP array_replace and the array_replace_recursive behavior on the collections.

With associative array keys, array_merge and array_replace behave the same way. However, with numeric keys/indexes, the merge appends the values to the array, but replace overrides the original value of the specific indexes.

@ahinkle
Copy link
Copy Markdown
Contributor

ahinkle commented Jul 5, 2019

Resubmit of #20220.

@iamgergo
Copy link
Copy Markdown
Contributor Author

iamgergo commented Jul 5, 2019

Yes, the functionality is the same, but this PR contains more tests for both functions.

Also, there is a suggestion to use map instead of replace. Well, it can be a good alternative for simple replacements, but for nested and recursive replacements it can be quite tricky.

The other problem with map is that, if the replacement array contains keys, that the original collection does not, we don't really have access to those items. The replace would append the new keys/values to the original collection.

Maybe using map and diff could solve this, but again, if it's a nested replacement, this issue is not solvable simply.

@taylorotwell taylorotwell merged commit 7eb7537 into laravel:5.8 Jul 6, 2019
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