使用php将数组键和值发送到csv

时间:2017-08-26 14:44:18

标签: php arrays json csv

所以我需要所有数组键成为excel电子表格中的第一列(第0列),并且数组的值将用于填充csv文件的其余列(第1-5列) )。

见下面的例子

代码:

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

我也尝试过以下内容并收到此错误,“数组到字符串转换”:

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

2 个答案:

答案 0 :(得分:0)

通常,当我创建CSV文件时,我只有一个数组:

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

但是如果你有一个单独的数组用于标题行和值:

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

使用简单数组当然可以简化这一过程,但您应该可以轻松传入数据。

最后,在你的情况下:

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

你也可以这样做,这会把日期添加到第一列:

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

答案 1 :(得分:0)

获取正确的元素

所以我们在这里使用三维数组,而不是二维数,我认为这是你的困惑所在。

// 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 
    ) 
)

修复此问题,您需要像这样修改最后一个循环:

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