Request: array_unique_values()
| From: | Colin Viebrock | Date: | Fri, 02 Jun 2000 21:15:33 +0000 |
| Subject: | Request: array_unique_values() | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-20099@lists.php.net to get a copy of this message | ||
I was looking through all the new array functions in PHP4 and couldn't find
one that would just return the unique values of a given array.
So if:
$a = array ( 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6 );
The function would return:
array ( 1, 2, 3, 4, 5, 6 );
This can be done in (at least) two ways in user-land:
function array_unique_values($a) {
return array_keys(array_count_values($a));
}
function array_unique_values($a) {
return array_flip(array_flip($a));
}
This could be problematic for associative arrays, but there are three
solutions:
(1) ignore key->value association and just assign 0,1,2, etc. to the
returned array
(2) keep the key->value association, but uses the key of the last unique
entry in the case of duplicates
(3) keep the key->value association, but uses the key of the first unique
entry in the case of duplicates
The two methods above are for the first 2 cases, respectively. I couldn't
find an "elegant" way of solving the 3rd.
Anyway ... since the code behind these array functions isn't internal PHP
code, I don't wanna mess with ext/standard/array.c myself. Anyone else feel
like adding array_unique_values() to PHP4? It seems pretty straightforward
... :)
__________________________________________________________________
Colin Viebrock easyDNS Technologies
Co-Founder control your domain
http://www.easyDNS.com