Notice: Array to string conversion

14,352
$list = array ($_POST["array"]);

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:

Array(0 =>
 Array(
   0 => "0",
   1 => "0",
   2 => "0",
   3 => "0",
   4 => "0",
   5 => "0",
   6 => "0",
   7 => "0",
   8 => 99
 )
)

To use the original array of strings, you probably just want:

$list = $_POST["array"];

Now, when you write print_r($list), you'll see the expected:

Array(
   0 => "0",
   1 => "0",
   2 => "0",
   3 => "0",
   4 => "0",
   5 => "0",
   6 => "0",
   7 => "0",
   8 => 99
)

BTW, use only .push for adding items to Javascript arrays, which you should instantiate like = [], not = new Array().

Share:
14,352
Xavier
Author by

Xavier

Updated on June 04, 2022

Comments

  • Xavier
    Xavier almost 2 years

    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 });
    
  • Xavier
    Xavier over 12 years
    God i love this site, Praise the lord for Stackoverflow.com
  • hakre
    hakre over 12 years
    Hallelujah! Let us praise the goddess of the fallen children!