Skip to content

Commit 05e9a48

Browse files
committed
Fix Collection unique() to handle numeric strings correctly
Fixes bug where SORT_REGULAR incorrectly deduplicates numeric strings when a '+' prefix is present. Collections like ['+19495551234', '9495551234', '19495551234'] now correctly return all three items instead of incorrectly removing the 11-digit format. The fix checks if the collection contains complex types (arrays/objects) that require SORT_REGULAR, or scalar values where default array_unique() is faster and more correct. This works around a documented PHP limitation with SORT_REGULAR where numeric strings are compared as numbers in certain contexts, causing unexpected deduplication behavior. See: php/doc-en#1463 Related to: 48a53be
1 parent be5d298 commit 05e9a48

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,14 @@ public function undot()
17711771
*/
17721772
public function unique($key = null, $strict = false)
17731773
{
1774-
if (is_null($key) && $strict === false) {
1775-
return new static(array_unique($this->items, SORT_REGULAR));
1774+
if ($key === null && $strict === false) {
1775+
foreach ($this->items as $item) {
1776+
if (! is_scalar($item) && $item !== null) {
1777+
return new static(array_unique($this->items, SORT_REGULAR));
1778+
}
1779+
}
1780+
1781+
return new static(array_unique($this->items));
17761782
}
17771783

17781784
$callback = $this->valueRetriever($key);

0 commit comments

Comments
 (0)