0

So I am needing all my array keys to be the first column(Column 0) in an excel spreadsheet and the values of the array will be used to fill in the rest of the columns of the csv file (Columns 1-5).

See below examples

Code:

        $array = array();

        foreach($k["Time)"] as $key => $value)
        {
            $array[] = [$value];                  
        }

        print_r($array);


        $file = fopen('demosaved.csv', 'w');




        fputcsv($file, array('Column 0','Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));


        $data = array_values($array);

        foreach ($data as $row)
        {
          fputcsv($file, $row);
        }

        fclose($file);

I have also tried the below and receive this error, "Array to string conversion":

        foreach($k["Time Series (1min)"] as $key => $value)
        {
            $array[] = [$key, $value];
        }


        var_dump($array);


        $file = fopen('demosaved.csv', 'w');




        fputcsv($file, array('Column 0','Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));


        $data = $array;


        foreach ($data as $row)
        {
          fputcsv($file, $row);
        }

        fclose($file);
6
  • so what is not working exactly? Commented Aug 26, 2017 at 14:58
  • only printing the array values to csv, I cannot get it to print the array keys to Column 0 along with the array values to Columns 1-5 Commented Aug 26, 2017 at 15:02
  • can you show the output of var_dump($array)? it will help immensely in finding where the issue is. Commented Aug 26, 2017 at 15:05
  • Array ( [0] => Array ( [0] => 2017-08-25 16:00:00 [1] => Array ( [1. open] => 141.9550 [2. high] => 141.9800 [3. low] => 141.9200 [4. close] => 141.9700 [5. volume] => 978667 ) ) [1] => Array ( [0] => 2017-08-25 15:59:00 [1] => Array ( [1. open] => 142.0200 [2. high] => 142.0300 [3. low] => 141.9500 [4. close] => 141.9575 [5. volume] => 369713 ) ) Commented Aug 26, 2017 at 15:10
  • I don't know if you already saw my answer, but I updated with your array. Hope that's what you wanted. I tested it on my end and it works. Commented Aug 26, 2017 at 15:24

2 Answers 2

1

Normally when I create CSV files I have only one array:

$array = array(
    array( 'a' => '1', 'b' => '2', 'c' => '3' ),
    array( 'a' => '4', 'b' => '5', 'c' => '6' ),
    array( 'a' => '7', 'b' => '8', 'c' => '9' ),
);

$file = fopen('demosaved.csv', 'wb');

fputcsv( $file, array_keys( $array[0] ) );

foreach( $array as $row )
{
    fputcsv( $file, array_values( $row ) );
}

fclose( $file );

But if you have a separate array for a header row and values:

$header = array(
    'foo', 'bar', 'baz'
);

$values = array(
    array( 'a' => '1', 'b' => '2', 'c' => '3' ),
    array( 'a' => '4', 'b' => '5', 'c' => '6' ),
    array( 'a' => '7', 'b' => '8', 'c' => '9' ),
);

$file = fopen('demosaved.csv', 'wb');

fputcsv( $file, $header );

foreach( $values as $row )
{
    fputcsv( $file, array_values( $row ) );
}

fclose( $file );

This is of course simplified by using simple arrays, but you should easily be able to pass in your data.

Finally, in your case:

$array = array(
    array(
        '2017-08-25 16:00:00',
        array(
            '1.open' => 141.9550,
            '2.high' => 141.9800,
            '3.low' => 141.9200,
            '4.close' => 141.9700,
            '5.volume' =>978667
        )
    ),
    array(
        '2017-08-25 15:59:00',
        array(
            '1.open' => 142.0200,
            '2.high' => 142.0300,
            '3.low' => 141.9500,
            '4.close' => 141.9575,
            '5.volume' => 369713
        )
    ),
);

$file = fopen('demosaved.csv', 'wb');

fputcsv( $file, array_keys( $array[0][1] ) );

foreach( $array as $row )
{
    fputcsv( $file, array_values( $row[1] ) );
}

fclose( $file );

You could also do something like this, which would add the date to the first column:

$array = array(
    array(
        '2017-08-25 16:00:00',
        array(
            '1.open' => 141.9550,
            '2.high' => 141.9800,
            '3.low' => 141.9200,
            '4.close' => 141.9700,
            '5.volume' =>978667
        )
    ),
    array(
        '2017-08-25 15:59:00',
        array(
            '1.open' => 142.0200,
            '2.high' => 142.0300,
            '3.low' => 141.9500,
            '4.close' => 141.9575,
            '5.volume' => 369713
        )
    ),
);

$file = fopen('demosaved.csv', 'wb');

$header_row = array_merge( array('Date' => NULL), $array[0][1] );

fputcsv( $file, array_keys( $header_row ) );

foreach( $array as $row )
{
    array_unshift( $row[1], $row[0] );
    fputcsv( $file, array_values( $row[1] ) );
}

fclose( $file );
Sign up to request clarification or add additional context in comments.

Comments

0

Getting the right elements

so we're working with a three dimensional array here, and not a two dimensional, which I think is where your confusion comes from.

// output of var_dump($array)
Array ( 
[0] => Array ( 
    [0] => 2017-08-25 16:00:00   // Column 0
    [1] => Array ( 
        [1. open] => 141.9550    // Column 1
        [2. high] => 141.9800    // Column 2
        [3. low] => 141.9200     // Column 3
        [4. close] => 141.9700   // Column 4
        [5. volume] => 978667    // Column 5
    ) 
) 
[1] => Array ( 
    [0] => 2017-08-25 15:59:00 
    [1] => Array ( 
        [1. open] => 142.0200 
        [2. high] => 142.0300 
        [3. low] => 141.9500 
        [4. close] => 141.9575 
        [5. volume] => 369713 
    ) 
)

Fix this issue, you'll need to modify the last loop like so:

foreach ($data as $row)
{
    $rowData = array_merge(array($row[0]), $row[1]); // this will flatten your array to one dimension
    fputcsv($file, $rowData);
}

3 Comments

Warning: array_merge(): Argument #1 is not an array in C:\xampp\htdocs\testing\test2.php on line 49 Warning: fputcsv() expects parameter 2 to be array, null given in C:\xampp\htdocs\testing\test2.php on line 50
My bad, I had spotted this on my editor but forgot to put it in the answer. I edited in the fix
@user7104290 no problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.