rgb192
asked on
remove duplicates from array
given an array
I want to remove duplicates
bob real estate
susan real estate
susan real estate
susan real estate
susan real estate
susan real estate
susan real estate
mary apartments
edward locators
I want to remove duplicates
bob real estate
susan real estate
mary apartments
edward locators
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
array unique is easiest
thanks
thanks
http://stackoverflow.com/questions/369602/how-to-delete-an-element-from-an-array-in-php
The page at the stackoverflow link contains examples, and explains how to remove elements from an array.
Basically, by calling unset() on the array element.
Then, after the duplicate elements are removed, you can reindex the array by calling the array_splice() function.
My suggestion for the duplicate removal logic is that you use a nested for loop, to look at each item in the array, and then for each item: loop through the array, searching for other items that are the same.
Then if you find a pair of different array elements that are the same, pick one of the two to delete.
A better alternative may be to scan for duplicates before populating the array; so, instead of removing duplicates, you avoid inserting duplicates in the first place --- this is what would generally be used.