带有标题

时间:2016-04-06 14:58:42

标签: php json csv curl

我从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);
}

2 个答案:

答案 0 :(得分:0)

你可能想要的是:

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

答案 1 :(得分:0)

在一位同事的帮助下,以及你们中的一些人的评论,这些对我的情况有用:

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