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
Next revisionBoth sides next revision
rfc:list_assoc_unique [2022/12/01 17:45] ilutovrfc:list_assoc_unique [2022/12/01 17:59] 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 49: Line 49:
  
 Internally, <php>array_unique()</php> sorts the array to avoid comparing each value with every other value which would Internally, <php>array_unique()</php> sorts the array to avoid comparing each value with every other value which would
-scale badly. For this reason, the second parameter <php>$flags</php> accepts the same <php>SORT_*</php> parameters as+scale badly. For this reason, the second parameter <php>$flags</php> accepts the same <php>SORT_*</php> options as
 <php>sort()</php> function and friends. <php>sort()</php> function and friends.
  
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