Skip to content
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

array_replace_recursive doesn't replace array filled with data by empty array #17377

Open
mho22 opened this issue Jan 6, 2025 · 4 comments
Open

Comments

@mho22
Copy link

mho22 commented Jan 6, 2025

Description

Based on array_replace_recursive php documentation :

array_replace_recursive(array $array, array ...$replacements): array

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:

<?php

$arr1 = [ 'foo' => [ 'bar' ]];
$arr2 = [ 'foo' => []];

var_dump(array_replace_recursive($arr1, $arr2));

Resulted in this output:

array(1) {
  ["foo"]=>
  array(1) {
    [0]=>
    string(3) "bar"
  }
}

But I expected this output instead:

array(1) {
  ["foo"]=>
  array(0) {
  }
}

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

@iluuu1994
Copy link
Member

This is indeed intended behavior.

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.

$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:
https://wiki.php.net/rfc/howto

The first step is to send an e-mail to the internals mailing list to see how your idea is received.
https://www.php.net/mailing-lists.php

@mho22
Copy link
Author

mho22 commented Jan 6, 2025

@iluuu1994 Thank you for your answer!

@MorganLOCode
Copy link

MorganLOCode commented Jan 8, 2025

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).

@mho22
Copy link
Author

mho22 commented Jan 8, 2025

@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.

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

No branches or pull requests

3 participants