1

I have a pretty big PHP Array: Lets say 2000 lines and 50 columns that all contain Strings.

I want to create a script that converts this array into a CSV file. With smaller arrays I did this by serializing the array and sending it inside a form in a hidden

<input name='csvarray' type='text' hidden value='<?php echo (serialize($csvArray)); ?>'>

with the form I am calling a php script through POST where I unserialize the array and create the .csv file.

The problem is that with bigger arrays I am running in some sort of Server problem because the input value (the serialized array of strings) gets bigger than 1.000.000 characters and the page crashes.

Any a bit more elegant way how to create a csv file on button click?

Thanks for any feedback!

MrWeix
  • 397
  • 1
  • 6
  • 17
  • 1
    Can you do it as a file upload? – Barmar Jan 24 '19 at 21:06
  • 5
    If you serialize the data using PHP, the server must already have access to the data. Either store it in a temp file or in a database until you need it again. It feels a bit strange to let the server serialize it in an input just to send it back. – M. Eriksson Jan 24 '19 at 21:06
  • What do you mean by "on a button click"? –  Jan 24 '19 at 21:26

2 Answers2

3

I think you're looking at it the wrong way. You already have this array on the server, so why do you need to send it to the client only to have it sent back to the server? Instead, I suggest you store this in session and use your hidden input to indicate that the array is actually in the session, something like this:

<?php
session_start();
$_SESSION['csvarray'] = $csvArray;
?>

<input name='csvarray' type='text' hidden value='1'>

Then, when processing your form, you'll have something like this:

session_start();
if (isset($_POST['csvarray'] && $_POST['csvarray'] == 1 && isset($_SESSION['csvarray'])) {
    $csvArray = $_SESSION['csvarray'];
}
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Wile I would go with your approach, it's also possible to create and attach file with javascript, if @MrWeix is set on doing it that way :) [this answer](https://stackoverflow.com/a/39854499/3226121) – ljubadr Jan 24 '19 at 21:36
  • 1
    I don't think it's wise to set massive amounts of data in the session storage. – Evert Jan 24 '19 at 21:38
  • @Evert I tend to agree, however it's much wiser than passing huge amounts of data between server and client back and forth. Plus, a a megabyte of data isn't a huge amount. – Aleks G Jan 24 '19 at 21:39
  • @MagnusEriksson fair point - but I'm simply dealing with what the OP has. As I mentioned above, it's still better than sending all this data back and forth between server and client. – Aleks G Jan 24 '19 at 21:43
  • Not arguing against that though :-) – M. Eriksson Jan 24 '19 at 21:44
0

Assuming your array is a 2d array...

$array = array(/* Whatever your array is */);
$csv = "";
foreach($array as $row) {
    foreach($row as $col) {
        $csv .= $col . ",";
    }
    $csv .= "\n";
}

This will create a csv that you can use however you want. No need to send to the client and back.

Edit: If your array has commas in it, you can use pre_replace()

        $csv .= preg_replace(",","\\,", $col) . ",";

Edit2: Someone has informed me that there is a function called fputcsv which renders my whole code useless. However, looking at it, it requires a file handle, so if just want a var with the csv, my code might be simpler.

  • What happens if any of the values contains commas or line breaks? Then the data will be messed up. You really shouldn't create CSV's manually. Use [fputcsv](http://php.net/manual/en/function.fputcsv.php) for creating csv. Since the data is just saved temporary on the server, using `serialize()` would be way easier. – M. Eriksson Jan 24 '19 at 21:44
  • Mm. Good point. That will mess up the whole thing. You would need to escape the commas, probably with something like pre_replace. –  Jan 24 '19 at 21:48
  • ...or you could use the function I linked to since it's a core PHP function that handles all that for you. And as mentioned, in this case, converting it to CSV doesn't seem necessary. Just store the results of `serialize()` and then use `unserialize()` to get it back as an array. – M. Eriksson Jan 24 '19 at 21:49
  • Sure, do it the easy way :D. I didn't see you link that at first, that's why I suggested preg_replace. –  Jan 24 '19 at 21:52
  • Fair enough :-) I know, I have a bad tendency to look for the easy (but still correct... most of the time) way to solve problems :-) – M. Eriksson Jan 24 '19 at 21:53
  • 1
    I mean, it's almost as if finding the simplest way to do things is a good idea! :D –  Jan 24 '19 at 21:59