PHP JSON to csv with headers

fdx2calv  于 12个月前  发布在  PHP
关注(0)|答案(2)|浏览(105)

我从一个cURL帖子中接收到JSON,它看起来像下面这样:

{"status":"ok","sent":177,"delivered":0,"bounced":5,"hardBounced":0,"softBounced":5,"opened":[46,81],"clicked":[5,5],"notsent":[2,2],"notopened":126,"optout":0,"spam":0,"lastOpen":"1 day ago","lastClick":"13 days ago","lastOpenTS":1459808038636,"lastClickTS":1458752521593,"rebroadcast":0,"rebroadcastClick":0,"msgId":"s-04ac-1603","subject":"Blah blah Conference and Exposition","title":"Blah followup 03.22.16","sentto":["l-160f"],"suppressedon":[]}

字符串
我正在编辑原始帖子,以显示在那些评论的帮助下取得的进展:
要查看从json类字符串到数组/对象的转换:

$result = curl_exec($ch_list);
$jsonObj = print_r(json_decode($result, true));
var_dump($jsonObj);die();


其产生以下结果:

Array
(
[status] => ok
[sent] => 177
[delivered] => 0
[bounced] => 5
[hardBounced] => 0
[softBounced] => 5
[opened] => Array
    (
        [0] => 46
        [1] => 81
    )

[clicked] => Array
    (
        [0] => 5
        [1] => 5
    )

[notsent] => Array
    (
        [0] => 2
        [1] => 2
    )

[notopened] => 126
[optout] => 0
[spam] => 0
[lastOpen] => 1 day ago
[lastClick] => 14 days ago
[lastOpenTS] => 1459808038636
[lastClickTS] => 1458752521593
[rebroadcast] => 0
[rebroadcastClick] => 0
[msgId] => s-04ac-1603
[subject] => AFSA Vehicle Finance Conference and Exposition
[title] => AFSA follow up 03.22.16
[sentto] => Array
    (
        [0] => l-160f
    )

[suppressedon] => Array
    (
    )

)


在这一点上,我想好了,我已经把它转换成一个数组,这应该会导致创建csv文件,我可以使用每个人都在这么多的职位推荐的代码,我确实收到了一个错误,但这是由fopen后的if语句纠正。所有这些都是现在创建一个csv文件,里面什么都没有:

$result = curl_exec($ch_list);
$jsonObj = print_r(json_decode($result));

if(!file_exists('/tmp/' . $msgId . '_' . $yearMonComb . '.csv')) {
    $f = fopen('/tmp/' . $msgId . '_' . $yearMonComb . '.csv', 'w');
    if (is_array($jsonObj) || is_object($jsonObj)) {
        $firstLineKeys = false;
        foreach ($jsonObj as $line) {
            if (empty($firstLineKeys)) {
                $firstLineKeys = array_keys($line);
                fputcsv($f, $firstLineKeys);
                $firstLineKeys = array_flip($firstLineKeys);
            }
            fputcsv($f, array_merge($firstLineKeys, $line));
        }
    }
    fclose($f);
}

ugmeyewa

ugmeyewa1#

你可能想要的是:

$result = curl_exec($ch_list);
// Decode the JSON representation into PHP data structures
// The second argument asks to return arrays (TRUE), not objects (FALSE or missing)
$data = json_decode($result, true);

$f = fopen($filename, 'w');

// Header line: the field names (keys in $data)
fputcsv($f, array_keys($data), ',');
// Data line (can use array_values($data) or just $data as the 2nd argument)
fputcsv($f, array_values($data), ',');

fclose($f);

字符串

aor9mmx1

aor9mmx12#

在一位同事的帮助下,以及你们中的一些人所做的评论,以下是适合我的情况的方法:

$result = curl_exec($ch_list);
$jsonObj = json_decode($result, true);

$f = fopen('/tmp/' . $msgId . '_' . $yearMonComb . '.csv', 'w');
fputcsv($f, array_keys($jsonObj));
$transposed = array();

if (is_array($jsonObj)) {
    foreach($jsonObj as $key => $record){
        if(is_array($record) && count($record) > 0){
            $target = count($record) - 1;
            $transposed[$key] = $jsonObj[$key][$target];
        }else{
            $transposed[$key] = $record;
        }
    }
    fputcsv($f, array_values($transposed));
}
fclose($f);

echo $result;
curl_close($ch_list);

字符串
这是执行print_r后的json数据:

Array
(
[status] => ok
[sent] => 177
[delivered] => 0
[bounced] => 5
[hardBounced] => 0
[softBounced] => 5
[opened] => Array
    (
        [0] => 46
        [1] => 81
    )

[clicked] => Array
    (
        [0] => 5
        [1] => 5
    )

[notsent] => Array
    (
        [0] => 2
        [1] => 2
    )

[notopened] => 126
[optout] => 0
[spam] => 0
[lastOpen] => 1 day ago
[lastClick] => 14 days ago
[lastOpenTS] => 1459808038636
[lastClickTS] => 1458752521593
[rebroadcast] => 0
[rebroadcastClick] => 0
[msgId] => s-04ac-1603
[subject] => Blah blah Conference and Exposition
[title] => Blah blah follow up 03.22.16
[sentto] => Array
    (
        [0] => l-160f
    )

[suppressedon] => Array
    (
    )

)


结果是一个398字节的CSV文件。
要回答@axiac的问题,使用数组以数组作为其值的情况。条件测试数组:

if(is_array($record) && count($record) > 0)


如果有数组,则获取数组的最后一个元素。
我希望这能帮助到一些人,因为这让我发疯了。
另一个编辑,因为我发现了一种方法,
我创建了一个单独的php文件来收集数组中的数组项--数组位于父数组的项$jsonObj“result”]中

$jsonObj = json_decode($result, true);
        $f = fopen($value . '-sent.csv', 'w');

        $headers = array('listid', 'name', 'when', 'recid', 'email', 'ts');

        if (is_array($jsonObj)) {
            fputcsv($f, $headers);
            foreach ($jsonObj['result'] as $fields) {
                fputcsv($f, $fields);
            }
        }
        fclose($f);

        curl_close($ch_list);

相关问题