Skip to content

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

Closed as not planned
@mho22

Description

@mho22

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions