...

View Full Version : get rid off the repeat word from an array



kamkam
11-11-2011, 10:30 PM
Hi;

How could i get rid off the repeat words from an array? Thanks

eg;

$a[]="the , think , this , that , the , their , that , the , the , those";

i just want the array reduce to "the, think, that,those".

Adee
11-11-2011, 11:05 PM
use array_unique() function

Spookster
11-11-2011, 11:12 PM
Use above. But if you were to do this the hard way you could do something like this:



<?php
$words = array('the', 'think', 'this', 'that', 'the', 'their', 'that', 'the', 'the', 'those');
$uniqueWords = array();
foreach ($words as $word) {
if (!in_array($word, $uniqueWords)) {
$uniqueWords[] = $word;
}
}
var_dump($uniqueWords);
?>

kamkam
11-12-2011, 12:58 AM
Thanks a lot



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum