简体   繁体   English

PHP数组打印意外的输出格式

[英]PHP array print unexpected output format

I am trying to print an array in reverse order in PHP 7, but it is printed in a strange way. 我试图在PHP 7中以相反的顺序打印数组,但是它以一种奇怪的方式打印。

<?php

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');

$output = "";

for($i = $n - 1; $i>=0; $i--){
    $output .= $arr[$i] . " ";
}

print($output);

?>

With the input: 用输入:

4
1 2 3 4

I get the output: 我得到的输出:

4
 3 2 1 

Why is that? 这是为什么?

如CodeBrauer所述,解决方案是在调用explode函数之前trim输入字符串:

$arr = explode(" ", trim($arr_temp));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM