[Solved] PHP array is having a gap when printed out


Your array 2nd value has newline char

See Demo

<?php

  $arr = array(1,'2'.PHP_EOL,3);
  print_r($arr);
  echo implode(" ", $arr);
?>

Would produce:

Array
(
    [0] => 1
    [1] => 2

    [2] => 3
)
1 2
 3

–edit–

Solution :

$arr = array_map('trim', $arr); after $arr = explode(" ", fgets($_fp)); because when reading a file using fgets() it will include the newline at the end. using trim() you can strip whitespace (or other characters) from the beginning and end of a string, you may use rtrim() which strips whitespace (or other characters) from the end of a string

1

solved PHP array is having a gap when printed out