JustinK101 Posted April 27, 2009 Share Posted April 27, 2009 I have an array: $results = array("abc", "abc", "def", "def", "ghi", "ghi"); Basically it has duplicate values. What the best way to remove the duplicates leaving only unique values? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/155790-remove-duplicates-from-array/ Share on other sites More sharing options...
mikesta707 Posted April 27, 2009 Share Posted April 27, 2009 I think there may be an easier way to do this, but the way i did it a while back was something like $array = ('dog', 'cat', 'mouse', 'dog', 'donkey'); $used = array(); $i=0; foreach($array as $key => $value){ $used[$i] = $value; if ($i != 0){ if (in_array($value, $used){ unset($array[$key]); } $i++; } There you go. might wanna check my syntax, but you get the basic idea hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/155790-remove-duplicates-from-array/#findComment-820092 Share on other sites More sharing options...
chronister Posted April 27, 2009 Share Posted April 27, 2009 http://us3.php.net/function.array-unique <?php $array = ('dog', 'cat', 'mouse', 'dog', 'donkey'); $noDuplicates = array_unique($array); echo '<pre>'; print_r($noDuplicates); echo '</pre>'; ?> Should give you Array ( [0] => dog [1] => cat [2] => mouse [4] => donkey ) Quote Link to comment https://forums.phpfreaks.com/topic/155790-remove-duplicates-from-array/#findComment-820107 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.