0

I am posting a large array to a PHP file. The array looks like this:

var arr = ["Filename", "Annotationtag", "Upper left corner X", "Upper left corner Y", "Lower right corner X", "Lower right corner Y", "locationName", 
    "/var/www/html/projects/MasterImages/95-20180527-190018-205342-00002.jpg", "TShirtBackSide_Pepsi", 435, 123, 504, 151, "TShirtBackSide"];

Once this array is received by the PHP script, I want to split this array at every 7th element, move the rest of the values to subsequent lines and eventually write the split array to a csv file where each line contains 7 columns of data.

A short html file is as follows:

arraypost.php

<html>
<head>
    <title>Test</title>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script type="text/javascript">
function pusharr() {
    var arr = ["Filename", "Annotationtag", "Upper left corner X", "Upper left corner Y", "Lower right corner X", "Lower right corner Y", "locationName", 
    "/var/www/html/projects/MasterImages/95-20180527-190018-205342-00002.jpg", "TShirtBackSide_Pepsi", 435, 123, 504, 151, "TShirtBackSide"];
    //Option1: var newarr = arr.toString();
    //Option2: var newarr = JSON.stringify();
    console.log(arr);
    $.post("getAnnotationsForTF.php", { 'data' : arr }, function(output){
          console.log(output);
          });
}
</script>
</head>

<body>
<button onclick="pusharr()">Click</button>
</body>
</html>

getAnnotationsForTF.php

<?php

$lsData = isset($_POST['data'])?$_POST['data']:'';
$inputArray = explode(',',$lsData);
$cnt =  count($inputArray);
$interval = $cnt/7;
$iArray = [];
$iArray = array_chunk($inputArray, $interval);
if(file_put_contents("files/test.csv", $iArray)) {
    echo "Working";
} else {
    echo "Not Working";
}
?>

While the csv file gets created, all I see in the csv file is:

ArrayArrayArrayArrayArrayArrayArrayArray

I tried various options. But not able to get through.

The desired output of csvfile is as follows:

Filename, Annotationtag, Upper left corner X, Upper left corner Y, Lower right corner X, Lower right corner Y, locationName
/var/www/html/projects/MasterImages/95-20180527-190018-205342-00002.jpg, TShirtBackSide_Pepsi, 435, 123, 504, 151, TShirtBackSide
5
  • Simple, file_put_contents() does not know how to write out an array! Commented Oct 11, 2018 at 17:05
  • Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything. Commented Oct 11, 2018 at 17:05
  • 1
    use fputcsv maybe? Commented Oct 11, 2018 at 17:06
  • Agree with @ArtisticPhoenix. fputcsv() will allow you to have each row as an array and just write the array directly to the file. Will require minimal effort for you to change, and it's designed for this exact issue :) Commented Oct 11, 2018 at 17:07
  • What are you using this for, you can save an actual PHP array in a file... and use it as an array in PHP.... :), using file_put_contents("files/test.php", '<?php return '.var_export($array, true).';') and then do $array = require 'files/test.php' Just saying... Commented Oct 11, 2018 at 17:10

1 Answer 1

3

Array Chunk returns a multidimensional array. You are writing that array to file.

Instead what you have to do:

$file = fopen("files/test.csv",'w');

foreach ($iArray as $row) {
    fputcsv($file, $row); 
}

Keep in mind fputcsv has the default delimiter ',' More info http://php.net/fputcsv

Sign up to request clarification or add additional context in comments.

5 Comments

Try adding FILE_APPEND as param 3 or use fputcsv as suggested above
Add $f=fopen("files/test.csv",'w') and fputcsv($row) Why create a CSV with file put when there is a function specifically made for it.
@sietse85 I am getting a blank file getting written...not sure if I am missing anything...tried both with the above given example code as well as my actual data.
fopen returns false on error, so if $file === false then for some reason the file could not be opened
@sietse85 Thank you. Your solution worked when I JSON.stringify ied the array at JS level. Whew! Tried so many combinations options since yesterday afternoon - without knowing where the issue was - working with arrays using JS and PHP is much clearer now. Thank you.

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.