Jump to content

Remove Duplicates From Array


JustinK101

Recommended Posts

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!

 

 

 

 

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

)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.