# array_unique optional compare-callback proposal

- Thread: https://externals.io/message/70060
- Original archive: https://news-web.php.net/php.internals/70060
- Started by Nikita Nefedov on 2013-11-07T13:24:44+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #70060 — Nikita Nefedov — 2013-11-07T13:24:44+00:00

URL: https://externals.io/message/70060#70060

Hello,

I would like to propose addition of a third (optional) argument for  
array_unique which will be callback for comparing elements.

There's a plenty of usecases for this. For example, what I've been dealing  
with can be described like so:

<?php

$obj1 = new EntityName("asd");
$obj2 = new EntityName("qwe");

$a = [new EntityWrapper($obj1, ...), new EntityWrapper($obj2, ...), new  
EntityWrapper($obj1, ...)];

// now it can be solved like so:

$newArray = [];
foreach ($a as $e) {
     $newArray[$e->getWrappedObject()->getIdentity()] = $e;
}

// but with this patch it can be solved with array_unique:

$newArray = array_unique($a, SORT_USERDEFINED, function ($a, $b) {
     return $a->getWrappedObject()->getIdentity() -  
$b->getWrappedObject()->getIdentity();
});


Actually my problem was a little different but you get the idea... And I  
think there will be even more usecases for this.

Here's a patch for master branch with implementation of this proposal:  
https://gist.github.com/nikita2206/7354392

---

## #70232 — Nikita Nefedov — 2013-11-20T11:30:45+00:00

URL: https://externals.io/message/70060#70232 · In reply to: #70060

On Thu, 07 Nov 2013 17:24:44 +0400, Nikita Nefedov <inefedor@gmail.com>  
wrote:

> Hello,
>
> I would like to propose addition of a third (optional) argument for  
> array_unique which will be callback for comparing elements.
>
> There's a plenty of usecases for this. For example, what I've been  
> dealing with can be described like so:
>
> <?php
>
> $obj1 = new EntityName("asd");
> $obj2 = new EntityName("qwe");
>
> $a = [new EntityWrapper($obj1, ...), new EntityWrapper($obj2, ...), new  
> EntityWrapper($obj1, ...)];
>
> // now it can be solved like so:
>
> $newArray = [];
> foreach ($a as $e) {
>      $newArray[$e->getWrappedObject()->getIdentity()] = $e;
> }
>
> // but with this patch it can be solved with array_unique:
>
> $newArray = array_unique($a, SORT_USERDEFINED, function ($a, $b) {
>      return $a->getWrappedObject()->getIdentity() -  
> $b->getWrappedObject()->getIdentity();
> });
>
>
> Actually my problem was a little different but you get the idea... And I  
> think there will be even more usecases for this.
>
> Here's a patch for master branch with implementation of this proposal:  
> https://gist.github.com/nikita2206/7354392

Hello internals!

As long as there were no objections against this proposal I would like to  
make it into the 5.6.
Should this go through all RFC steps or can I just create a PR for it and  
expect it to be merged?

---

## #70242 — Stas Malyshev — 2013-11-20T19:02:39+00:00

URL: https://externals.io/message/70060#70242 · In reply to: #70232

Hi!

> > […]

What I notice here is that if we add SORT_USERDEFINED, we need to add it
to all other sorts, otherwise it would be inconsistent - why SORT_*
works for all sort functions but SORT_USERDEFINED works only for one of
them? And that opens a bit of a can of worms because for many array
functions this already exists as u* aliases - usort, array_udiff, etc.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #70245 — Nikita Nefedov — 2013-11-20T20:16:30+00:00

URL: https://externals.io/message/70060#70245 · In reply to: #70242

On Wed, 20 Nov 2013 23:02:39 +0400, Stas Malyshev <smalyshev@sugarcrm.com>  
wrote:

> Hi!
>
> > […]
>
> What I notice here is that if we add SORT_USERDEFINED, we need to add it
> to all other sorts, otherwise it would be inconsistent - why SORT_*
> works for all sort functions but SORT_USERDEFINED works only for one of
> them? And that opens a bit of a can of worms because for many array
> functions this already exists as u* aliases - usort, array_udiff, etc.
>

Hi,

Actually I have considered implementing another function for this, but  
there were two things I didn't like: name of the function array_uunique -  
it just sounds bad and it will cause a lot of mistakes (when you use  
autocomplete especially), and second - this function will have a lot of  
copy-pasted stuff from array_unique source (though this is somewhat  
solvable if I create another function with logic (not exposed to  
userland)).

But what do you think about this signature then:
array_unique(array $array, int $flag);
array_unique(array $array, Callable $callback);

This is, again, seems inconsistent with all other array_u* functions, but  
calling a function 'array_uunique' is just something I would prefer to  
avoid.
