2012-09-13 6 views
3

array_splice를 사용하여 배열의 배열에 배열을 삽입하려고합니다.이 PHP 오류 : fputcsv()는 매개 변수 2가 배열이 될 것으로 예상합니다

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

편집 : 여기

가 인 print_r ($ 배열)을 추가 한 후 CSV 파일의 첫 번째 행이 될해야하는지에 대한 배열의 섹션 직전 outputCSV ($ array). "[124] => q6_3_3_2"다음에 위치 0에 삽입 된 배열이 닫히지 않고 하위 배열이있는 것처럼 보입니다.

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 
+1

outputCSV 함수의'var_dump ($ vals)'는 무엇을 보여줍니까? – andrewsi

+1

또는'outputCSV ($ array) '를 호출하기 바로 전에'print_r ($ array)' – Crontab

+0

위의 print_r ($ array) 결과를 추가했습니다. – Rynardt

답변

1

배열 데이터를 게시 해 주셔서 감사합니다. 문제는 배열이 다차원이라는 것입니다. 나는. 당신은 125에서 '하위 배열'을 가지고 있습니다.

docs (http://php.net/manual/en/function.fputcsv.php)를 보면 명확하지 않지만 다차원 배열을 사용할 수는 없습니다 fputcsv와. 생각해 보면, 전달한 배열은 csv에서 한 줄로 변환됩니다.

데이터 구조와 CSV 형식으로 데이터 구조를 예상하고 이에 따라 코드를 수정해야합니다.