3

I am trying to insert an array into an array of arrays using array_splice.

array_splice($array, 0, 0, $fieldNames);

The resulting array is then converted to csv format with fputcsv. I get the following error:

 fputcsv() expects parameter 2 to be array  string given in...

What am I doing wrong? Must be some minor bug. Please any help. Also am I taking the right approach. My PHP knowledge of functions is not huge.

Full php code:

<?php

//MySQL login details
require('sqlAuth.php');

//return error data
function errorReport($error)
{
    $data = array("error" => $error);
    $json = json_encode($data);
    echo $json;
    die();
}

//export array to csv file
function outputCSV($data) {
    $outstream = fopen("php://output", "w");
    function __outputCSV(&$vals, $key, $filehandler) {
        fputcsv($filehandler, $vals); // add parameters if you want
    }
    array_walk($data, "__outputCSV", $outstream);
    fclose($outstream);
}

// open connection 
$connection = mysql_connect($host, $user, $pass) or errorReport("Unable to Connect"); 

// select database 
mysql_select_db($db, $connection) or errorReport("Unable to Select Database: " .mysql_error()); 

//build result set array
$array = array();

//get full questions table
$query = "SELECT q.q_id, q.trip_day, q.q0_1, q.q0_2, q.q0_3, q.q0_4, q.q0_5, q.q0_6, g.address, g.latitude, g.longitude, g.method, q.q1_1, q.q1_2, q.q1_3, q.q1_7, q.q1_9, q.q1_10, q.q1_11_1, q.q1_11_2, q.q1_11_3, q.q1_11_4, q.q2_6, q.q6_0,
                 q.q6_1, q.q6_1_1_1, q.q6_1_1_2, q.q6_1_1_3, q.q6_1_1_4, q.q6_1_1_5, q.q6_1_1_6, q.q6_1_1_7, q.q6_1_1_8, q.q6_1_1_9, q.q6_1_1_10, q.q6_1_1_11, q.q6_1_1_12, q.q6_1_1_13, q.q6_1_1_14, 
                 q.q6_1_1_15, q.q6_1_1_16, q.q6_1_2_1, q.q6_1_2_2, q.q6_1_2_3, q.q6_1_2_4, q.q6_1_2_5, q.q6_1_2_6, q.q6_1_2_7, q.q6_1_2_8, q.q6_1_2_9, q.q6_1_2_10, q.q6_1_2_11, q.q6_1_2_12, q.q6_1_2_13, 
                 q.q6_1_2_14, q.q6_1_2_15, q.q6_1_2_16, q.q6_1_3_1, q.q6_1_3_2, q.q6_2, q.q6_2_1_1, q.q6_2_1_2, q.q6_2_1_3, q.q6_2_1_4, q.q6_2_1_5, q.q6_2_1_6, q.q6_2_1_7, q.q6_2_1_8, q.q6_2_1_9, 
                 q.q6_2_1_10, q.q6_2_1_11, q.q6_2_1_12, q.q6_2_1_13, q.q6_2_1_14, q.q6_2_1_15, q.q6_2_1_16, q.q6_2_2_1, q.q6_2_2_2, q.q6_2_2_3, q.q6_2_2_4, q.q6_2_2_5, q.q6_2_2_6, q.q6_2_2_7, 
                 q.q6_2_2_8, q.q6_2_2_9, q.q6_2_2_10, q.q6_2_2_11, q.q6_2_2_12, q.q6_2_2_13, q.q6_2_2_14, q.q6_2_2_15, q.q6_2_2_16, q.q6_2_3_1, q.q6_2_3_2, q.q6_3, q.q6_3_1_1, q.q6_3_1_2, 
                 q.q6_3_1_3, q.q6_3_1_4, q.q6_3_1_5, q.q6_3_1_6, q.q6_3_1_7, q.q6_3_1_8, q.q6_3_1_9, q.q6_3_1_10, q.q6_3_1_11, q.q6_3_1_12, q.q6_3_1_13, q.q6_3_1_14, q.q6_3_2_1, q.q6_3_2_2, q.q6_3_2_3, 
                 q.q6_3_2_4, q.q6_3_2_5, q.q6_3_2_6, q.q6_3_2_7, q.q6_3_2_8, q.q6_3_2_9, q.q6_3_2_10, q.q6_3_2_11, q.q6_3_2_12, q.q6_3_2_13, q.q6_3_2_14, q.q6_3_3_1, q.q6_3_3_2 
                 FROM questions AS q LEFT JOIN gmap_address_list AS g ON q.q0_7 = g.id";
$result_questions = mysql_query($query) or errorReport("Error in query: $query. ".mysql_error());
while ($row = mysql_fetch_array($result_questions, MYSQL_ASSOC)) {
    $array[] = $row;
}

//get field names
$fieldNames = array();
$fieldNum = mysql_num_fields($result_questions);
for ($i = 0; $i < $fieldNum; $i++) {
    $fieldNames[] = mysql_field_name($result_questions, $i);
}
array_splice($array, 0, 0, $fieldNames);

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=emmhts_questionnaires.csv");
header("Pragma: no-cache");
header("Expires: 0");
outputCSV($array);

// free result set memory
mysql_free_result($result_questions);

//close connection to mysql db
mysql_close($connection);
?>

Edit:

Here is a section of the array for what should be the first row of the csv file after adding print_r($array) just before outputCSV($array). It looks like the array inserted in position 0 is not closed after "[124] => q6_3_3_2", but rather there is a sub array.

Array
(
    [0] => q_id
    [1] => trip_day
    [2] => q0_1
    [3] => q0_2
    [4] => q0_3
    [5] => q0_4
    [6] => q0_5
    [7] => q0_6
    [8] => address
    [9] => latitude
    [10] => longitude
    [11] => method
    [12] => q1_1
    [13] => q1_2
    [14] => q1_3
    [15] => q1_7
    [16] => q1_9
    [17] => q1_10
    [18] => q1_11_1
    [19] => q1_11_2
    [20] => q1_11_3
    [21] => q1_11_4
    [22] => q2_6
    [23] => q6_0
    [24] => q6_1
    [25] => q6_1_1_1
    [26] => q6_1_1_2
    [27] => q6_1_1_3
    [28] => q6_1_1_4
    [29] => q6_1_1_5
    [30] => q6_1_1_6
    [31] => q6_1_1_7
    [32] => q6_1_1_8
    [33] => q6_1_1_9
    [34] => q6_1_1_10
    [35] => q6_1_1_11
    [36] => q6_1_1_12
    [37] => q6_1_1_13
    [38] => q6_1_1_14
    [39] => q6_1_1_15
    [40] => q6_1_1_16
    [41] => q6_1_2_1
    [42] => q6_1_2_2
    [43] => q6_1_2_3
    [44] => q6_1_2_4
    [45] => q6_1_2_5
    [46] => q6_1_2_6
    [47] => q6_1_2_7
    [48] => q6_1_2_8
    [49] => q6_1_2_9
    [50] => q6_1_2_10
    [51] => q6_1_2_11
    [52] => q6_1_2_12
    [53] => q6_1_2_13
    [54] => q6_1_2_14
    [55] => q6_1_2_15
    [56] => q6_1_2_16
    [57] => q6_1_3_1
    [58] => q6_1_3_2
    [59] => q6_2
    [60] => q6_2_1_1
    [61] => q6_2_1_2
    [62] => q6_2_1_3
    [63] => q6_2_1_4
    [64] => q6_2_1_5
    [65] => q6_2_1_6
    [66] => q6_2_1_7
    [67] => q6_2_1_8
    [68] => q6_2_1_9
    [69] => q6_2_1_10
    [70] => q6_2_1_11
    [71] => q6_2_1_12
    [72] => q6_2_1_13
    [73] => q6_2_1_14
    [74] => q6_2_1_15
    [75] => q6_2_1_16
    [76] => q6_2_2_1
    [77] => q6_2_2_2
    [78] => q6_2_2_3
    [79] => q6_2_2_4
    [80] => q6_2_2_5
    [81] => q6_2_2_6
    [82] => q6_2_2_7
    [83] => q6_2_2_8
    [84] => q6_2_2_9
    [85] => q6_2_2_10
    [86] => q6_2_2_11
    [87] => q6_2_2_12
    [88] => q6_2_2_13
    [89] => q6_2_2_14
    [90] => q6_2_2_15
    [91] => q6_2_2_16
    [92] => q6_2_3_1
    [93] => q6_2_3_2
    [94] => q6_3
    [95] => q6_3_1_1
    [96] => q6_3_1_2
    [97] => q6_3_1_3
    [98] => q6_3_1_4
    [99] => q6_3_1_5
    [100] => q6_3_1_6
    [101] => q6_3_1_7
    [102] => q6_3_1_8
    [103] => q6_3_1_9
    [104] => q6_3_1_10
    [105] => q6_3_1_11
    [106] => q6_3_1_12
    [107] => q6_3_1_13
    [108] => q6_3_1_14
    [109] => q6_3_2_1
    [110] => q6_3_2_2
    [111] => q6_3_2_3
    [112] => q6_3_2_4
    [113] => q6_3_2_5
    [114] => q6_3_2_6
    [115] => q6_3_2_7
    [116] => q6_3_2_8
    [117] => q6_3_2_9
    [118] => q6_3_2_10
    [119] => q6_3_2_11
    [120] => q6_3_2_12
    [121] => q6_3_2_13
    [122] => q6_3_2_14
    [123] => q6_3_3_1
    [124] => q6_3_3_2
    [125] => Array
        (
            [q_id] => 29
            [trip_day] => Thursday
            [q0_1] => 4
            [q0_2] => 5
            [q0_3] => 5
            [q0_4] => 5
            [q0_5] => 5
            [q0_6] => 0732152589
Rynardt
  • 5,547
  • 7
  • 31
  • 43

1 Answers1

1

Thanks for posting your array data. The problem is your array is multidimensional. I.e. you have a 'subarray' at 125.

Looking at the docs (http://php.net/manual/en/function.fputcsv.php) it isn't clear, but you can't use multidimensional arrays with fputcsv. If you think about it, the array you pass will be converted to one line in the csv.

You will need to think about the structure of your data, and how you expect it to be formatted in your csv, and modify your code accordingly.

Luke Mills
  • 1,616
  • 1
  • 11
  • 18