0

I am trying to echo certain letters of a string where if a letter has already been echoed it cannot be echoed again.

Here is my current code:

<?php
$string = 'AABACADA';
    echo $string[1];
    echo $string[3];
    echo $string[4];
    echo $string[5];
    echo $string[6];
    echo $string[7];
?>

The result of this code is 'AACADA'. But I want to change the code so that a letter can only be echoed once, so the code's result should be 'ACD'.

I'm honestly stuck and would really appreciate some help. Thank you.

Harry
  • 19
  • 1
  • 3

2 Answers2

3

You could use count_chars($string, $mode) and set the $mode to 3.

Check out the doumentation: http://php.net/count_chars

Shawn Northrop
  • 5,826
  • 6
  • 42
  • 80
0

you can try this. I am not sure it is what you are looking for.

$string = 'AABACADA';
$tmp = [];
for ($i = 0; $i < strlen($string); $i++) {
  if ($i === 2) continue;
  $tmp[] = $string[$i];
}

print_r(array_unique($tmp));

Result: Array ( [0] => A [3] => C [5] => D )