0

I want to give an array via POST to a php script which should save the array to a "map"-file.

$data = $_POST["dat"];
for($y = 0; $y < 37; $y++){
       for($x = 0; $x < 37; $x++){
              $dat .= $data[($y*37)+$x] . ",";
       }
       $dat .= "\n";
}

But the file is like this:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

But i want this result:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,

Why are there empty lines between each line??

3
  • 1
    Try to change out: $data[($y*37)+$x] with trim($data[($y*37)+$x]) Commented Aug 24, 2015 at 14:14
  • Is there empty lines when you var_dump $dat? In other words: try to find out if the problem occurs in the code you pasted (doesn't look like it) or occurs when writing the file. Commented Aug 24, 2015 at 14:18
  • 1
    @Rizier123 , works, pls make a answer so i can mark is as solved Commented Aug 24, 2015 at 14:19

1 Answer 1

1

So as it seems you already have some new line characters in your input. To simply remove them just use trim(), e.g.

$dat .= trim($data[($y*37)+$x]) . ",";
      //^^^^^ See here
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I don't get it, how can this be a solution? If he had new lines in the input, lines would be broken somewhere in the middle before the comma, with a new line directly below starting with a comma. It's not what I see in OP's example.
@rlanvin Why do they need to be in the middle before the comma? They can be anywhere, but OP has them in the pattern, so that after the first line he has a new line in each other new input line.
The comma is concatenated after the input. If your input contained "0\n" as you are suggesting, the resulting string should be "0\n," so you would see a line break and then a comma at the beginning of the next line.
True, that would explain. But it's more common the other way around, and it would have to be exactly every 37 inputs and nowhere else. Well why not... kudos for finding out.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.