Skip to content

Arr::forget may delete incorrect keys due to reference not being reset before exists() check #58866

@adacnvwp

Description

@adacnvwp

Laravel Version

laravel 12

PHP Version

8.5.3

Database Driver & Version

No response

Description

I encountered unexpected behavior in Arr::forget when deleting multiple keys in a single call. The issue appears to be caused by $array not being reset to the original reference before calling static::exists() inside the foreach loop.
Expected Result
[
'user' => [
'test' => 'user.test',
],
]
Actual Result
[
'user' => [],
'test' => 'test'
]
The $array variable may still reference a nested sub-array from the previous iteration (due to $array = &$array[$part]; inside the while loop). Since $array is not reset to $original before the exists() check, the existence check may be performed on the wrong array level.

Steps To Reproduce

$array = [
'user' => [
'name' => 'user.name',
'test' => 'user.test',
],
'test' => 'test'
];

    Arr::forget($array, ['user.name', 'test']);
    return $array;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions