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 17:46] 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 77: 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