I am getting an error when posting via ajax to a csv file via php.
<?php
$list = array ($_POST["array"]);
$fp = fopen('array.csv', 'w');
fputcsv($fp, $list);
fclose($fp);
?>
My array i am trying to post
["0", "0", "0", "0", "0", "0", "0", "0", 99]
The response:
Notice: Array to string conversion in C:\xampp\htdocs\snx\assets\www\write.php on line 4
My Ajax post;
/* Array */
var defaultArray = new Array();
var localArray = new Array();
var serverArray = new Array();
/* Default Values */
defaultArray[0] = "0";
defaultArray[1] = "0";
defaultArray[2] = "0";
defaultArray[3] = "0";
defaultArray[4] = "0";
defaultArray[5] = "0";
defaultArray[6] = "0";
defaultArray[7] = "0";
defaultArray.push(99);
/* Write Array [1st Load] */
$.post("write.php", { 'array': defaultArray });
If
$_POST['array']is already an array,Array($_POST['array'])makes an array of [one] arrays of strings.If you write
print_r($list), you will see something like this:To use the original array of strings, you probably just want:
Now, when you write
print_r($list), you’ll see the expected:BTW, use only
.pushfor adding items to Javascript arrays, which you should instantiate like= [], not= new Array().