You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
When the value in the first array and the second array are both arrays, array_replace_recursive() will replace their respective value recursively.
If this can be a feature request, proposing adding an optional flag to allow replacing non-empty arrays with empty ones ?
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.
In the meantime array_replace, the nonrecursive version of array_replace_recursive, doesn't go into nested arrays to replace their contents and will instead replace arrays with arrays (in the example given, resulting in the intended result).
@MorganLOCode You're right. But when recursion is needed, this option is not possible. A custom method is needed using array_replace in that situation.
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: