Home>

It doesn't work to remove whitespace and line feed code after converting an array to a string.
If i check the code of the output expected in chrome, it is as follows.

"1700 30"
<br>
"1200 80"
<br>
"900 110"
<br>
"900 10"
<br>
"800 20"

However, in the code I wrote, it looks like this in chrome.

"1700 30"
<br>
"1200 80"
<br>
"900 110"
<br>
"900 10"
<br>
"800 20"
<br>

A space and a line feed code are added after 20.
It was written on the net that trim should be used, but it had no effect.

Corresponding source code

Input value
2000 5
300
500
300
100
100

while ($input_line = fgets (STDIN)) {
   $tmp [] = trim ($input_line);
}
    $line = $tmp [0];
    $line2 = explode ('', $line);
    $line2 [1];
    $zan = $line2 [0];
    $point = 0;
    for ($i = 1;$i<= $line2 [1];$i ++) {
       if ($point<$tmp [$i]) {
        $point + = $tmp [$i] * 1/10;
        $zan = $zan-$tmp [$i];
       }
       if ($point>= $tmp [$i]) {
           $point- = $tmp [$i];
       }
       $answer [] = $zan.''. $Point;
    }
    $answer2 = implode (PHP_EOL, $answer);
    $answer2 = trim ($answer2);
    echo $answer2;


Contents of $answer
array (
0 =>'1700 30',
1 =>'1200 80',
2 =>'900 110',
3 =>'900 10',
4 =>'800 20',
)

By the way
$answer2 = implode ("a", $answer);
If i check the source with chrome
"1700 30a1200 80a900 110a900 10a800 20"

  • Answer # 1

    It seems that the cause was that there was a space after. Thank you for responding while the explanation did not go well.

  • Answer # 2

    If you don't want to add
    at the end, you can simply implode with
    .

    <? php
    $answer = array (
    0 =>'1700 30',
    1 =>'1200 80',
    2 =>'900 110',
    3 =>'900 10',
    4 =>'800 20',
    );
    echo'<p>'. PHP_EOL;
    echo implode ('<br>'. PHP_EOL, $answer);
    echo'</p>'. PHP_EOL;

  • Answer # 3

    print_r ($answer)

    What is the result of?

    I feel like there is something in it only at the end.