-
Notifications
You must be signed in to change notification settings - Fork 7.8k
array_replace_recursive doesn't replace array filled with data by empty array #17377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is indeed intended behavior.
I think this is quite niche, and it still does not allow for solve the problem when a nested, non-empty array is intended to become the full replacement of some other value. This could be solved by introducing some wrapper class as a marker. $arr1 = [ 'foo' => [ 'bar' ]];
$arr2 = [ 'foo' => new ArrayReplaceRecursiveValue([])];
var_dump(array_replace_recursive($arr1, $arr2)); It's not particularly pretty, but at least it would solve the problem in a more generic way. Anyway, if you'd like to propose this or some other solution, you can check how the RFC process works here: The first step is to send an e-mail to the internals mailing list to see how your idea is received. |
@iluuu1994 Thank you for your answer! |
In the meantime |
@MorganLOCode You're right. But when recursion is needed, this option is not possible. A custom method is needed using |
There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests. |
Description
Based on
array_replace_recursive
php documentation :I suppose the second argument is an array that probably have to replace the first one. Trying to replace a current array filled with
bar
by an empty array won't do anything. Basically because when it comes to arrays, the function will go in it and process data, but since there is no data it leaves the array and continues its work.So technically, it is not possible with
array_replace_recursive
to replace a non-empty array by an empty array.The following code:
Resulted in this output:
But I expected this output instead:
If this is not a bug and this behavior is intended, I am sorry.
If this can be a feature request, proposing adding an optional flag to allow replacing non-empty arrays with empty ones ?
I would be happy to contribute if this is considered a valid feature request.
In any case, thank your time and for all your hard work.
PHP Version
PHP 8.3.15
Operating System
No response
The text was updated successfully, but these errors were encountered: