从PHP数组写入CSV文件 [英] Write a CSV file from a PHP Array

查看:493
本文介绍了从PHP数组写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很高兴与此主题相关的问题很多.但是,由于我不是PHP开发人员,所以我正在努力使适用于我的特定对象结构的任何东西.我有一个相当平坦的JSON结构,可以将其读入PHP.我需要做的就是遍历对象数组,从第一个对象中的键创建CSV标题(它们都是相同的),然后使用所有对象属性编写CSV的每一行.看一下我的PHP数据:

I appreciate there are already a lot of questions around this subject. But, as I'm not a PHP developer, I'm struggling to get anything to work for my specific object structure. I have a pretty flat JSON structure that is being read into PHP just fine. All I need to do is go through the array of objects, create the CSV headings from the keys in the first object (they are all the same), and then write each line of the CSV using all the object properties. Here's a look at my PHP data:

Array
(
    [0] => stdClass Object
        (
            [record id] => -KA9S-beTbe9LIPBm_xK
            [timestamp] => Wed Feb 10 2016 10:12:20 GMT+0100
            [user id] => 33037t23nxyx1x5k
            [user age] => 18-24
            [user gender] => M
            [user q1] => Yes
            [user q2] => Yes
            [user q3] => more
            [answer q1 value] => 1
            [answer q1 confidence] => 3
            [answer q1 duration] => 262412
            [answer q2 value] => 1
            [answer q2 confidence] => 3
            [answer q2 duration] => 1959
            [answer q3 value] => 0
            [answer q3 confidence] => 3
            [answer q3 duration] => 2939
            [answer q4 value] => 1
            [answer q4 confidence] => 2
            [answer q4 duration] => 1868
            [answer q5 value] => 1
            [answer q5 confidence] => 2
            [answer q5 duration] => 2196
        )

因此,您可以看到它应该非常简单.键将是CSV单元格标题,然后是每个记录的一行.

So, you can see it should be pretty straightforward. The keys would be the CSV cell headings and then a row for each record.

我希望有人能帮助我.我一点都不了解PHP.

I hope someone can help me. I don't know PHP at all.

更新:这是我最新的PHP文件.这行得通.我必须将对象强制转换为数组以获取键:

UPDATE: Here's my latest PHP file. This works. I had to cast the Object to an Array to get the Keys:

<?php

    $data = $_POST['data'];
    $response = json_decode($data);

    $records_arr = $response->records;

    $fp = fopen('records.csv', 'w');
    $i = 0;

    foreach ($records_arr as $record) {


        // $record is an object so create an array
        $record_arr = array();

        foreach ($record as $value) {
            $record_arr[] = $value;
        }

        if($i == 0){
            fputcsv($fp, array_keys((array)$record));
        }
        fputcsv($fp, array_values($record_arr));
        $i++;
    }

    fclose($fp);

    echo print_r($records_arr);
?>

感谢大家的帮助.很棒的社区!

Thank you everyone for the help. Amazing community!

推荐答案

要将PHP数组写入CSV文件,您应该使用内置的PHP函数fputcsv:

For writing a PHP array to a CSV file you should use the built in PHP function fputcsv:

<?php

$list = array (
    array('header 1', 'header 2', 'header 3', 'header 4'),
    array('5656', '454545', '5455', '5454'),
    array('541212', '454545', '5455', '5454'),
    array('541212', '454545', '5455', '5454'),
);

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

foreach ($list as $fields) {
    fputcsv($fp, $fields);
}

fclose($fp);

根据您的数据输入更具体的示例:

More specific example based on your data input:

<?php

// I had to recreate your data, this is just an example
$array_of_objects = array();
for( $i = 0; $i < 5; $i++ )
{
    $obj = new \stdClass();
    $obj->username = "Rein";
    $obj->answer = "Correct";
    $obj->awesomeness = 1000;

    $array_of_objects[] = $obj;
}

// Open a file to write to
$fp = fopen('file.csv', 'wb');

$i = 0;

// Loop the array of objects
foreach( $array_of_objects as $obj )
{
    // Transform the current object to an array
    $fields = array();

    foreach ($obj as $k => $v)
    {
        $fields[ $k ] = $v;
    }

    if( $i === 0 )
    {
        fputcsv($fp, array_keys($fields) ); // First write the headers
    }

    fputcsv($fp, $fields); // Then write the fields

    $i++;
}

fclose($fp);

基于新信息(您首先需要一个JSON字符串),我添加了此示例,作为将数据写入CSV文件的更好方法:

Based on new information (you have a JSON string to start with), I'm added this example as a better way to write the data to a CSV file:

<?php

$data = json_decode($_POST['data'], TRUE);
$records = $data['records'];

$fp = fopen('records.csv', 'wb');

$i = 0;
foreach ($records as $record) {

    if($i === 0) {
        fputcsv($fp, array_keys($record));
    }

    fputcsv($fp, array_values($record));
    $i++;
}

fclose($fp);

这篇关于从PHP数组写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆