2

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 "\r" but the result is nothing in cmd. Please can you help?

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • I am not doing your homework ;) But you should have a look at the `$total` variable, where is it declared? And you are echo'ing the complete `$data` array from inside your loop. Have a look at this: https://www.w3schools.com/php/php_looping_for.asp – Erik van de Ven Oct 24 '18 at 12:30
  • 1
    Sorry it is not clear how you want the output to look. Can you edit the question and show your expected output please – RiggsFolly Oct 24 '18 at 12:30
  • yeah show the word in one line alternately – Aliga Myway Oct 24 '18 at 12:30
  • 1
    On *nixy systems you can do something like: echo $data . " \r"; – jaxwilko Oct 24 '18 at 12:31
  • 4
    @JackWilko Or to work on anything `echo $data . PHP_EOL;` – RiggsFolly Oct 24 '18 at 12:32
  • @RiggsFolly `PHP_EOL` is useless nowadays though where `\n` works on all systems. – Xatenev Oct 24 '18 at 12:34
  • 1
    @RiggsFolly no, you misunderstand, just printing " \r" will update the current line rather than adding a new line, i.e. for loading bars – jaxwilko Oct 24 '18 at 12:35
  • @Xatenev Oh, does `"\n"` work on all systems. I didn't know that – RiggsFolly Oct 24 '18 at 12:35
  • @JackWilko Ahhhh! I wonder if that is what the OP is trying to say? You could be right!! Hmmmmm – RiggsFolly Oct 24 '18 at 12:37
  • @AligaMyway Can you just show the expected output instead of an explanation of the expected output? E.g. **Wanted output:** `a/b/c` – Xatenev Oct 24 '18 at 12:39
  • imagine that in line 3 of cmd will show the word alternately (a -> b -> c) sorry my english is bad – Aliga Myway Oct 24 '18 at 12:42
  • So you want line 1 to output "a", line 2 to output "a -> b" and line 3 to output "a -> b -> c"? – Stefmachine Oct 24 '18 at 12:44
  • What if OP is looking something similar to a loading feedback on which each array item is displayed as a feedback to a loading sequence? Like $data = ['loading x','loading y','loading z']; It then makes sense when he asks to show them alternatively (just guessing BTW) – Ace_Gentile Oct 24 '18 at 12:44
  • Hi @AligaMyway, I added a gif to show what I think you're trying to do. Is that right? – Don't Panic Oct 24 '18 at 13:30

3 Answers3

5

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
}
Erik van de Ven
  • 4,747
  • 6
  • 38
  • 80
-1

do like this:

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

go with foreach like below:

foreach($data as $d) {
   echo "\033[1D";
    echo $d;
    sleep(1);
}
Hiren Siddhpura
  • 189
  • 1
  • 8
-1

I hope this is what you want...

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

for ($i=0; $i < $total; $i++){
    echo $data[$i] . "\n";
}
Barry
  • 3,303
  • 7
  • 23
  • 42
Niraj Kaushal
  • 1,452
  • 2
  • 13
  • 20