user5299698
user5299698

Reputation:

php printing array element with space

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.

Upvotes: 1

Views: 1414

Answers (2)

Anandhu Nadesh
Anandhu Nadesh

Reputation: 672

ltrim will remove spaces from left side

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

Upvotes: 0

Jigar Shah
Jigar Shah

Reputation: 6223

use ltrim() it will remove space from left end

read documentation for further details

Upvotes: 1

Related Questions