Echo在一行但在循环情况下

I have the following code:

$data  = ['a','b','c'];
$total = count($data);
for($i = 0; $i < $total; $i++) {
    echo $data;
}

What I need is when the code is running in cmd, the cmd will output each character so that it replaces the previous character in the display, like this:

enter image description here

a >> then changing to show b >> then changing to show c

...only in one line.

imagine that in line 3 of cmd will show the word alternately (a -> b -> c)

I have tried with "" but the result is nothing in cmd. Please can you help?

I suppose this is what you want. Correct me if I'm wrong. It first prints a, then replaces it with b, then replaces it with c.

$data  = ['a','b','c'];
$total = count($data);
for($i = 0; $i < $total; $i++) {
    echo "\033[1D";      // Move 1 character backward
    echo $data[$i];  
    sleep(1);           // wait for a while, so we see the animation
}

do like this:

$data = ['a','b','c'];

go with foreach like below:

foreach($data as $d) {
   echo "\033[1D";
    echo $d;
    sleep(1);
}

I hope this is what you want...

$data = ['a','b','c'];
$total = count($data);

for ($i=0; $i < $total; $i++){
    echo $data[$i] . "
";
}