array unique

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

array unique

Post by php_east »

i have an array

Code: Select all

[]AAA
[]aaa
[]BBB
[]bbb
[]CCC
[]ccc
 
i want to end up only with

Code: Select all

 
[]AAA
[]BBB
[]CCC
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array unique

Post by Mark Baker »

Do an array_walk() to set every entry as uppercase, then array_unique()
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: array unique

Post by php_east »

maybe my example wasn't so good. what if i have this...

Code: Select all

[]AaA
[]aAa
[]BBb
[]bbB
[]cCC
[]ccC
 
i want

Code: Select all

[]AaA
[]BBb
[]cCC
 
the reason for this BTW is because people type in mixed cases.
if i can pick a unique set, i can then process accordingly.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: array unique

Post by php_east »

@mark,

yes, i tried, something similar, with lowercasing everthing, but i want my originals 'AaA' back and it got so unbelievably complex that i thought i's better ask. it seems trivial yet i ended with some 10 lines.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: array unique

Post by php_east »

Code: Select all

$words          = array_unique($words);
 
$minimum_word_length    = 4;
$small_words        = array_map('strtolower',$words);
$small_words        = array_unique($small_words);
foreach ($small_words as $key=>$small_word)
{
if (strlen($small_word)<$minimum_word_length) continue;
if (array_key_exists($key, $words)) 
    {
    $unique_words[] = $words[$key];
    }
}
 
$words  = $unique_words;
 
this works, but hmmmph...., fat.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array unique

Post by Mark Baker »

Maybe not the cleanest method:

Code: Select all

 
$testArray = array('AaA',
        'AAA',
        'aaa',
        'aAa',
        'BBB',
        'bbB',
        'Bbb',
        'Ccc',
        'CCC'
        );
 
function arrayCaseFilter($item, $key) {
    global $testArray;
 
    foreach($testArray as $filterKey => $filterEntry) {
        if ((strtoupper($item) == strtoupper($filterEntry)) && ($key != $filterKey)) {
            unset($testArray[$filterKey]);
        }
    }
}
 
array_walk($testArray,'arrayCaseFilter');
 
print_r($testArray);
 
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: array unique

Post by php_east »

at least it looks shorter than mine. will go and tinker around with it a bit and see if i can shorten it further. many thanks Mark.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: array unique

Post by Mark Baker »

Just as a quirk, I'd expected the following:

Code: Select all

 
function arrayCaseFilter($item, $key, &$filterArray) {
    foreach($filterArray as $filterKey => $filterEntry) {
        if ((strtoupper($item) == strtoupper($filterEntry)) && ($key != $filterKey)) {
            unset($filterArray[$filterKey]);
        }
    }
}
 
array_walk($testArray,'arrayCaseFilter',$testArray);
 
to work.

It's a cleaner alternative than using the global
But for some reason, it simply returns $testArray completely unchanged.
Can't fathom why, perhaps somebody else can provide an explanation
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: array unique

Post by php_east »

managed to get it down to some two lines, taking some ideas from your posted codes...

Code: Select all

 
$buffer = array_unique(array_map('strtolower',$words));
$words  = array_intersect_key ($words,$buffer);
 
strtolower or strtoupper is fine, my preference is lower.

p/s actually you should not change an array size inside of callback, the results are unpredictable.
somehow the global did allow that to happen, but i could not sleep well. and the revised array walk did not work due to that fact i think.

also, i tried

Code: Select all

$filterArray=$testArray;
array_walk($testArray,'arrayCaseFilter',$filterArray);
print_r($filterArray);
 
and my apache crashed ! 'LOL.
Post Reply