rfc:list_assoc_unique

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
rfc:list_assoc_unique [2022/12/01 14:24] ilutovrfc:list_assoc_unique [2022/12/01 18:01] ilutov
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-The <php>array_unique</php> function allows getting all unique values of a given array. Unfortunately, PHP has multiple+The <php>array_unique()</php> function allows getting all unique values of a given array. Unfortunately, PHP has multiple
 definitions of equality and thus uniqueness. The most obvious one (i.e. <php>$a === $b</php>) is not supported by definitions of equality and thus uniqueness. The most obvious one (i.e. <php>$a === $b</php>) is not supported by
-<php>array_unique</php>.+<php>array_unique()</php>.
  
 This RFC proposes adding two new functions, <php>List\unique()</php> and <php>Assoc\unique()</php> as alternatives This RFC proposes adding two new functions, <php>List\unique()</php> and <php>Assoc\unique()</php> as alternatives
 using strict equality (<php>===</php>) semantics, the former discarding and the latter preserving keys. using strict equality (<php>===</php>) semantics, the former discarding and the latter preserving keys.
  
-<php>+<code php>
 List\unique([1, 2, 3, 1, '2', 3.0, new Foo, ['bar']]) List\unique([1, 2, 3, 1, '2', 3.0, new Foo, ['bar']])
-> [1, 2, 3, '2', 3.0, Foo, ['bar']]+// > [1, 2, 3, '2', 3.0, Foo, ['bar']]
  
 Assoc\unique(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'foo']) Assoc\unique(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'foo'])
-> ['foo' => 'foo', 'bar' => 'bar'+// > ['foo' => 'foo', 'bar' => 'bar'
-</php>+</code>
  
 ===== Proposal ===== ===== Proposal =====
Line 41: Line 41:
 Removing duplicates from arrays is a common use case provided by many programming languages. PHPs <php>array_unique()</php> Removing duplicates from arrays is a common use case provided by many programming languages. PHPs <php>array_unique()</php>
 has been there for [[https://github.com/php/php-src/commit/99f151f1bde0483944767cf0ed49d3270f61d326|~23 years]]. has been there for [[https://github.com/php/php-src/commit/99f151f1bde0483944767cf0ed49d3270f61d326|~23 years]].
-However, PHP has multiple definitions of equality, four in particular supported by <php>array_unique</php>:+However, PHP has multiple definitions of equality, four in particular supported by <php>array_unique()</php>:
  
   * <php>SORT_STRING</php> - Converts values to strings and compares with <php><</php>/<php>==</php>/<php>></php>   * <php>SORT_STRING</php> - Converts values to strings and compares with <php><</php>/<php>==</php>/<php>></php>
Line 48: Line 48:
   * <php>SORT_LOCALE_STRING</php> - Converts values to strings and compares with ''strcoll''   * <php>SORT_LOCALE_STRING</php> - Converts values to strings and compares with ''strcoll''
  
-Arguablythe most straight forward of those in terms of coercion is <php>SORT_STRING</php>Howevernone of these +Internally, <php>array_unique()</php> sorts the array to avoid comparing each value with every other value which would 
-options support arrays and objects, and all of them are subject to subtle coercion issues. Additionally, coercion can +scale badlyFor this reasonthe second parameter <php>$flags</php> accepts the same <php>SORT_*</php> options as 
-lead to warnings that are likely undesirable, as the fact that these values are coerced for comparison is an +<php>sort()</php> function and friends. 
-implementation detail.+ 
 +None of these options support arrays and objects, and other primitive types are subject to subtle coercion issues. 
 +Additionally, coercion can lead to warnings that are likely undesirable, as the fact that these values are coerced for 
 +comparison is an implementation detail.
  
 ==== Keys ==== ==== Keys ====
Line 74: Line 77:
 ===== Alternative approaches ===== ===== Alternative approaches =====
  
-Previously, adding a new parameter to <php>array_unique()</php> was discussed. The discussion has revealed that most +Previously, adding a new <php>ARRAY_UNIQUE_IDENTICAL</php> constant that can be passed to <php>array_unique()</php>s <php>$flag</php> 
-people would prefer a new function over fixing the existing function with a new parameter that might be more difficult +parameter was discussed. The discussion has revealed that most people would prefer a new function over extending <php>array_unique()</php> 
-to discover.+with a flag that might be more difficult to discover.
  
 ===== Vote ===== ===== Vote =====
rfc/list_assoc_unique.txt · Last modified: 2022/12/01 18:19 by ilutov