1

here is my code, I want to print single space between each array element problem i am facing is m getting space on every element but there is space for 1st element also

$handle = fopen ("php://stdin","r");
fscanf($handle,"%d",$n);
$arr_temp = fgets($handle);
$arr = explode(" ",$arr_temp);
array_walk($arr,'intval');
for($i=sizeof($arr);$i>=0;$i--)
{

  echo $arr[$i]." ";

} 

 ?>

my output is " 2 3 4 1" i want "2 3 4 1" there is space in the 1st element.

1
  • Add isset($arr[$i])&&$arr[$i]!=""?$arr[$i]." ":"" to ignore not set and blank value. Commented Jul 11, 2017 at 4:34

2 Answers 2

1

use ltrim() it will remove space from left end

read documentation for further details

Sign up to request clarification or add additional context in comments.

2 Comments

@pranavmandlik please mark the answer as accepted so other developers know it has been answered.
yes sure but its saying you can accept answer in 10 minutes
0

ltrim will remove spaces from left side

echo ltrim($arr[$i])." ";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.