You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Many of you may be blissfully unaware, but PHP's SORT_REGULAR is fundamentally flawed and leads to unpredictable results, especially when used with array_unique() as it's currently implemented in Collection->unique().
I submitted a PR to the Laravel framework with a partial attempt to fix the issue. After much consideration, I feel we need to take SORT_REGULAR behind the woodshed and accept correctness over micro optimization. But we don't have to revert to the original in_array -- there are better solutions.
If we all prefer to be typestict for Collection->unique() by default, we can be both fast and correct. The alternative, is a little slowdown by default (non-typestrict by default), but correct, as opposed to the current fast and mostly kinda correct.
return new static(array_unique($this->items, SORT_REGULAR));
instead of return new static(array_unique($this->items)); ?
UPDATE
Maybe because the second one would call __toString on each model and thus json encode the model and that is slower.
But a simple online search reveals:
Using array_unique($this->items, SORT_REGULAR) on an array of objects is unreliable and may produce unexpected results. This is because array_unique() internally sorts the array to efficiently detect duplicates, but PHP objects are not comparable using < or >. As a result, the sort order becomes inconsistent or undefined, causing duplicates to be missed. Even if objects are identical in content or are the same instance, SORT_REGULAR does not guarantee correct deduplication due to this sorting instability. The behavior can vary depending on array size and object types, making it appear random.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Many of you may be blissfully unaware, but PHP's
SORT_REGULARis fundamentally flawed and leads to unpredictable results, especially when used witharray_unique()as it's currently implemented inCollection->unique().I submitted a PR to the Laravel framework with a partial attempt to fix the issue. After much consideration, I feel we need to take
SORT_REGULARbehind the woodshed and accept correctness over micro optimization. But we don't have to revert to the originalin_array-- there are better solutions.If we all prefer to be typestict for
Collection->unique()by default, we can be both fast and correct. The alternative, is a little slowdown by default (non-typestrict by default), but correct, as opposed to the current fast and mostly kinda correct.See: #57480
Also see my new PR that at least gives us a performant solution for us who can't accept
SORT_REGULAR: #575173 votes ·
All reactions