Skip to content

Collection::filter() retains indices. #12

Closed
@b00gizm

Description

@b00gizm

When using the Collection::filter() method, the resulting collection retains the indices of the filtered out items, which may not yield the expected behavior when using ArrayAccess functionality.

$c = new Collection([0, 1, 2]);
$filtered = $c->filter(fn ($i) => $i % 2);

The expected behavior would be that $filtered[0] == 0 and $filtered[1] == 2, but only the former is true, since it retains the indices of the original array, i.e. $filtered[2] == 2 is true, even though count($filtered) == 2.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions