1

I'm trying to save a multidimensional Javascript array as a CSV file on the server. So far, my code creates the CSV file, but the file doesn't contain my array, and I'm not sure why.

(where outputData is established)

function dataSave(){
outputStr = JSON.stringify(outputData);
$.ajax({
    type: "POST",
    url: "csvout.php",
    data: outputStr,
    dataType: 'html',
});}

csvout.php:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<?php    
$array = json_decode($_POST['outputStr']);
$file = fopen("out/data.csv","w");
foreach ($array as $line){
    foreach ($line as $item){
        fputcsv($file,explode(',',$item));
        }
    }

fclose($file); 
?>

</head>
<body>
</body>
</html>

The data is an array of arrays like this one-- it's data obtained from the client, who is participating in a scientific experiment. Everything is a string.

0: "tuba.mp3"
1: "prac"
2: "1"
3: "0"
4: "type1"
5. "500"

0: "clap.mp3"
1: "main"
2: "0"
3: "0"
4: "type1"
5. "300"

[...]
5
  • Could we have an example of how your array may look like? Commented Apr 11, 2016 at 16:14
  • Re: using fputcsv and explode at the same time, I'm a little confused by this answer because there is example code at this link that uses both and it seems to work: w3schools.com/php/func_filesystem_fputcsv.asp Commented Apr 11, 2016 at 16:18
  • Add "jQuery" to your question tags; you may get more relevant help if you specify what your code is about and it will result in less "down-votes". Commented Apr 11, 2016 at 16:19
  • Printing it to console reads Array [ Array[11], Array[11], Array[11], Array[11], Array[11], Array[11], Array[11], Array[11], Array[11], Array[11], 6 more… ]. Commented Apr 11, 2016 at 16:20
  • @Chris do var_dump($array) after you've assigned it in PHP and update your question with the result (or a summary of the result, I need to know how deep the array is). Commented Apr 11, 2016 at 16:21

1 Answer 1

1

You are passing a raw JSON string as your query parameter but are attempting to access it as a named parameter. Updated code:

function dataSave(){
outputStr = JSON.stringify(outputData);
$.ajax({
    type: "POST",
    url: "csvout.php",
    data: { outputStr: outputStr },
    dataType: 'html',
});}


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<?php    
$array = json_decode($_POST['outputStr']);
adding var_dump($array); here produces NULL

$file = fopen("out/data.csv","w");
foreach ($array as $line){
    fputcsv($file,$item);
}

fclose($file); 
?>

</head>
<body>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this does work, but it is saving the data in a single column when opened in Excel, instead of with each nested array as a row. Would there be a simple way to fix that?
I've made a small update. I still don't know how your data looks like exactly, but if it's just an array of arrays, this should work.

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.