|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Arr_Dupl_Del() : Array - Delete duplicates while retaining original element orderThis function deletes similar elements in an array while maintaining the original order of the elements. This built-in PHP function array_unique() removes duplicates, but it also sorts the elements, which is sometimes undesirable.function Arr_Dupl_Del($Arr, $CaseSensitive = true) {
/*
Purpose
Used to remove duplicate elements from an array without changing the order of the elements.
Parameters
Arr (array) = The array we're working with.
CaseSensitive (optional boolean) = Determines if elements with differing case are considered duplicates.
If True, similar elements with different cases are considered different.
If False, similar elements with different cases are considered the same and are deleted.
Default: True
Return (array)
Returns the specified array (Arr) with all duplicates removed.
When duplicate elements exist, the first duplicate remains and all later-occurring ones are removed.
Version History
1.0 2012.11.15 Tested by Russ Tanner on PHP 5.3.8.
*/
$r = array();
if ($CaseSensitive) foreach($Arr as $x) { if (! in_array($x, $r)) $r[] = $x; }
else { foreach($Arr as $x) { if (array_search(strtolower($x), array_map('strtolower', $r)) === false) { $r[] = $x; } } }
return $r;
}
|
|
|
|
|