3

我正在尝试使用 array_splice 将数组插入到数组数组中。

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

然后使用 fputcsv 将生成的数组转换为 csv 格式。我收到以下错误:

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

我究竟做错了什么?一定是一些小错误。请任何帮助。我也采取了正确的方法。我对函数的 PHP 知识并不丰富。

完整的php代码:

<?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);
?>

编辑:

这是在 outputCSV($array) 之前添加 print_r($array) 之后应该是 csv 文件的第一行的数组的一部分。看起来插入位置 0 的数组在“[124] => q6_3_3_2”之后没有关闭,而是有一个子数组。

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
4

1 回答 1

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.

于 2012-09-18T09:15:55.410 回答