1

My array looks like this;

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
)

How can I output it to a csv in three columns? One for the Month name, and the other two for the two dates that follow.

At the moment my code looks like this;

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

The headers go in fine, but I don't know how to get three columns on the data. At the moment at array goes in to the csv all in one column.

I thought array_chuck() might help - but I couldn't work it out.

mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95
  • Is it possible for you to make your array() to a multidimensional array ? Like array("January" => array("2015-01-30", "2015-01-15")) – D3F Apr 03 '15 at 19:37

3 Answers3

1

This should work for you:

Here I just change the structure from:

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
)

to:

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
        )

)

I do this with splitting the array into chunks of 3 with array_chunk(). After this I just go through the header array and take each array_column() and put it in the $data array. After this you can simply loop through the array as you did and write each line in your .csv file.

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

?>

output:

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

EDIT:

I think I missed something; So this should be what you want:

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

?>

output:

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
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Closer than I've managed! Thanks. But your solution creates 12 columns and 3 rows. I wanted 3 columns on 12 rows. – mikelovelyuk Apr 03 '15 at 19:53
  • @mike3875 What, how? Your array only has 9 elements! So it's just creates 3 rows with 3 columns each. What array do you really use and what output would you expect? – Rizier123 Apr 03 '15 at 19:54
  • @mike3875 I think I see it now what you mean, updated my answer – Rizier123 Apr 03 '15 at 20:03
  • @mike3875 So was my edit the solution or another answer? Or where are we? If nothing helped you please make sure you show us your **real** array and your expected output – Rizier123 Apr 03 '15 at 20:40
  • yeah sorry about that. I meant 9. Your updated answer got it correct! – mikelovelyuk Apr 03 '15 at 20:45
0

Clean up the data... I see Rizier123's answer now... probably cleaner. Anyway crux of what everyone is saying, get the array the way you want it, then worry about the 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]=> ...
ficuscr
  • 6,975
  • 2
  • 32
  • 52
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, as you've mentioned, is used, and works fine in this example.

sinisake
  • 11,240
  • 2
  • 19
  • 27