Page 1 of 1
array unique
Posted: Thu Mar 26, 2009 8:21 am
by php_east
i have an array
Code: Select all
[]AAA
[]aaa
[]BBB
[]bbb
[]CCC
[]ccc
i want to end up only with
Re: array unique
Posted: Thu Mar 26, 2009 8:57 am
by Mark Baker
Do an array_walk() to set every entry as uppercase, then array_unique()
Re: array unique
Posted: Thu Mar 26, 2009 9:03 am
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
the reason for this BTW is because people type in mixed cases.
if i can pick a unique set, i can then process accordingly.
Re: array unique
Posted: Thu Mar 26, 2009 9:06 am
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.
Re: array unique
Posted: Thu Mar 26, 2009 9:48 am
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.
Re: array unique
Posted: Thu Mar 26, 2009 9:49 am
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);
Re: array unique
Posted: Thu Mar 26, 2009 9:51 am
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.
Re: array unique
Posted: Thu Mar 26, 2009 10:30 am
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
Re: array unique
Posted: Thu Mar 26, 2009 1:12 pm
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.