PHP Function arrayAddSortNoDupes(array $arr, mixed $itemToAdd)
PHP Function arrayAddSortNoDupes Removes Duplicates from Array, Checks if is Already in the Array, if not Adds it and Sorts the Array
Home
Short:
/* -------------------------------------------------------------- add item $itemToAdd to array $arr if its not already there returned value is $arr with duplicates removed and sorted -------------------------------------------------------------- */ function arrayAddSortNoDupes(array $arr, mixed $itemToAdd) { # add the new value to 4arr $arr[] = $itemToAdd; # how many items in $arr $originalCount = count($arr); # remove duplicates $arr = array_unique($arr); # if after array_unique if count($arr) # changes then we removed duplicates $isDupe = ($originalCount > count($arr)); # sort the array values in order and re-index $arr sort($arr); return [$arr, $isDupe]; }
source code home