Re: array_unique optional compare-callback proposal

From: Date: Wed, 20 Nov 2013 11:30:45 +0000
Subject: Re: array_unique optional compare-callback proposal
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-70232@lists.php.net to get a copy of this message
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?

Thread (4 messages)

« previous php.internals (#70232) next »