UPDATE
After playing around with the code I can display the string as an array and print it which is displaying the following
Array ( [0] => anagram ) Array ( [0] => anagram ) Array ( [0] => anagram ) Array ( [0] => anagram ) Array ( [0] => anagram ) Array ( [0] => anagram )
If this is a multidimensional array how can I get it to not print duplicates?
Thanks for taking the time to read this.
The problem im having is the following code will take one word and function it is a permutation, the echoed results are duplicated. This happens whenever the word contains duplicate letters. When it swaps a letter with a copy of itself, such as the first and third letters in anagram, the new word is also in the dictionary, so it gets printed again.
<?php
function permute($str, $l, $r, $pspell_link)
{
if ($l == $r) {
if (pspell_check($pspell_link, $str)) {
echo "<p>".ucwords($str)."</p>";
};
}
else
{
for ($i = $l; $i <= $r; $i++)
{
$str = swap($str, $l, $i);
permute($str, $l + 1, $r, $pspell_link);
$str = swap($str, $l, $i);
}
}
}
function swap($a, $i, $j)
{
$temp;
$charArray = str_split($a);
$temp = $charArray[$i] ;
$charArray[$i] = $charArray[$j];
$charArray[$j] = $temp;
return implode($charArray);
}
$str = "anagram";
$n = strlen($str);
$pspell_link = pspell_new("en");
permute($str, 0, $n - 1, $pspell_link);
?>
Basicly in simple terms this is what i expecting to happen
<?php
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);
foreach ($result as $result) {
echo "<p>".ucwords($result)."</p>";
}
?>
which would echo out (green red blue)
I have tried using
return array_unique(explode(' ', $str));
Ive also tried
for ($i = $l+1; $i <= $r; $i++)
{
if ($str[$l] == $str[$l]) {
continue;
}
$str = swap($str, $l, $i);
permute($str, $l + 1, $r, $pspell_link);
$str = swap($str, $l, $i);
}
No matter how much i play around i cant seem to find a solution to remove duplicate words.