2d数组到fputcsv无法正常工作

时间:2015-04-03 19:34:53

标签: php arrays multidimensional-array fputcsv

我的数组看起来像这样;

Array
(
 [0] => January
 [1] => 2015-01-30
 [2] => 2015-01-15
 [3] => February
 [4] => 2015-02-27
 [5] => 2015-02-18
 [6] => March
 [7] => 2015-03-31
 [8] => 2015-03-18
)

如何将其输出到三列中的csv?一个用于月份名称,另外两个用于后面的两个日期。

目前我的代码看起来像这样;

$header = array("Month","Date One","Date Two");

$fp = fopen($filename, "w");
fputcsv ($fp, $header, "\t");
foreach($payments_array as $row){
     fputcsv($fp, array($row), "\t");
}
fclose($fp);

标题很好,但我不知道如何在数据上获得三列。目前,数组在一列中进入csv。

我认为array_chuck()可能会有所帮助 - 但我无法解决这个问题。

3 个答案:

答案 0 :(得分:1)

这应该适合你:

这里我只是改变结构:

Array
(
    [0] => January
    [1] => 2015-01-30
    [2] => 2015-01-15
    [3] => February
    [4] => 2015-02-27
    [5] => 2015-02-18
    [6] => March
    [7] => 2015-03-31
    [8] => 2015-03-18
)

为:

Array
(
    [0] => Array
        (
            [0] => January
            [1] => February
            [2] => March
        )

    [1] => Array
        (
            [0] => 2015-01-30
            [1] => 2015-02-27
            [2] => 2015-03-31
        )

    [2] => Array
        (
            [0] => 2015-01-15
            [1] => 2015-02-18
            [2] => 2015-03-18
        )

)

我这样做是将数组拆分为3 array_chunk()的块。在此之后,我只需浏览标题数组并取出每个array_column()并将其放入$data数组中。在此之后,您可以像您一样循环遍历数组,并在.csv文件中写下每一行。

<?php

    $arr = array_chunk($arr, 3);

    foreach($arr[0] as $k => $v)
        $data[] = array_column($arr, $k);


    $fp = fopen($filename, "w");
    foreach($data as $row){
         fputcsv($fp, $row, "\t");
    }
    fclose($fp);

?>

输出:

January February    March
2015-01-30  2015-02-27  2015-03-31
2015-01-15  2015-02-18  2015-03-18

修改

我想我错过了什么;所以这应该是你想要的:

<?php

    $data = array_chunk($arr, 3);
    $header = array("Month","Date One","Date Two");

    $fp = fopen($filename, "w");
    fputcsv($fp, $header, "\t");
    foreach($data as $row){
        fputcsv($fp, $row, "\t");
    }
    fclose($fp);

?>

输出:

Month   "Date One"  "Date Two"
January 2015-01-30  2015-01-15
February    2015-02-27  2015-02-18
March   2015-03-31  2015-03-18

答案 1 :(得分:0)

清理数据......我现在看到Rizier123的答案......可能更干净了。无论如何,每个人都在说的话,按照你想要的方式获得数组,然后担心fputcsv。

$payments_array = [
 'January',
 '2015-01-30',
 '2015-01-15',
 'February',
 '2015-02-27',
 '2015-02-18',
 'March',
 '2015-03-31',
 '2015-03-18'];

$better_payments_array = [];
for ($i=0; $i<count($payments_array); $i++) {   
    $better_payments_array[] = [$payments_array[$i], $payments_array[$i+1],     $payments_array[$i+2]]; 
    $i=$i+2;
}

var_dump($better_payments_array);

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(7) "January"
    [1]=>
    string(10) "2015-01-30"
    [2]=>
    string(10) "2015-01-15"
  }
  [1]=> ...

答案 2 :(得分:0)

$array=array( 'January','2015-01-30','2015-01-15','February','2015-02-27','2015-02-18','March','2015-03-31','2015-03-18');

$header = array("Month","Date One","Date Two");
$chunks=array_chunk($array,3);
$fp = fopen('test.csv', "w");
fputcsv ($fp, $header, "\t");
for($i=0;$i<count($chunks);$i++) {


    fputcsv($fp, $chunks[$i], ',');


}
fclose($fp);
正如您所提到的,

array_chunk已被使用,并且在此示例中正常运行。