0

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.

Mal
  • 5
  • 6
  • Your last bit is almost correct (I think) should be `if ($str[$l] == $str[$i]) {` (You have `$l` in both sides in your code). – Nigel Ren Aug 22 '19 at 16:06
  • @NigelRen The only problem adding the if continue it doesn't echo any results at all, im really puzzled. – Mal Aug 22 '19 at 16:15
  • Have you tried changing the second `$l` to `$i`? – Nigel Ren Aug 22 '19 at 16:16
  • @NigelRen Yes i tried what you recommended – Mal Aug 22 '19 at 16:22
  • This isn't going to answer your question, but I highly suggest you avoid single-letter variable names (except when used in a `for` loop). It makes it incredibly difficult to tell what you're trying to do. Recommended is to use descriptive variable names. For example, instead of `$a` you could do `$word`. This is especially important because even if we can figure out what your code is doing, non-descriptive variable names make it difficult to tell what your intentions are. Just something for you to think about as you continue to write code. – B. Fleming Aug 22 '19 at 17:49

0 Answers0