【问题标题】:Export Json Array to CSV in php在 php 中将 Json 数组导出为 CSV
【发布时间】:2015-12-17 18:59:23
【问题描述】:

我有这个提供者数组

[
    {
        "reference":"01042",
        "images":   [
            "http:\/\/static1.provider.com\/34\/01042.jpg"
        ]
    },
    {
        "reference":"01057",
        "images":[
            "http:\/\/static1.provider.com\/57\/01057.jpg",
            "http:\/\/static3.provider.com\/58\/01057.jpg",
            "http:\/\/static2.provider.com\/59\/01057.jpg"]
    },
    ...
]

我用下面的代码导出

$json_file2 = file_get_contents('http://direct.provider.com/public/ref_urlimage_20.json', false);

$decoded = json_decode($json_file2);

$fp = fopen('imagenes.csv', 'w');
foreach($decoded as $comment) {
    fputcsv($fp, $comment);
}
fclose($fp);

但它显示了以下结果

01104,Array
01119,Array
40460,Array
00311,Array
00312,Array
00307,Array

当你需要导出到这种格式时

01104,http://static3.provider.com/155/01119.jpg
01119,http://static3.provider.com/155/04519.jpg,http://static3.provider.com/155/01148.jpg,http://static3.provider.com/155/0859.jpg
40460,http://static3.provider.com/155/01119.jpg,http://static3.provider.com/155/01118.jpg
00351,http://static3.provider.com/175/07219.jpg
...

我哪里做错了? 谢谢

【问题讨论】:

    标签: php json fputcsv


    【解决方案1】:
    if (empty($argv[1])) die("The json file name or URL is missed\n");
    $jsonFilename = $argv[1];
    
    $json = file_get_contents($jsonFilename);
    $array = json_decode($json, true);
    $f = fopen('output.csv', 'w');
    
    $firstLineKeys = false;
    foreach ($array as $line)
    {
        if (empty($firstLineKeys))
        {
            $firstLineKeys = array_keys($line);
            fputcsv($f, $firstLineKeys);
            $firstLineKeys = array_flip($firstLineKeys);
        }
        $line_array = array($line['reference']);
        foreach ($line['images'] as $value)
        {
            $line_array.push($value);
        }
        fputcsv($f, $line_array);
    
    }
    

    因为你有数组里面循环像上面的代码可能有助于解决问题

    试试下面的代码就行了

    <?php
    //if (empty($argv[1])) die("The json file name or URL is missed\n");
    //$jsonFilename = $argv[1];
    //
    //$json = file_get_contents($jsonFilename);
    $json_file2 = file_get_contents('http://direct.funidelia.es/public/ref_urlimage_20.json', false);
    error_reporting(E_ALL);
    //echo $json_file2;die;
        $json='{"data":'.$json_file2.'}';
    //echo $json;
    $array = json_decode($json, true);
    //echo "<pre>";
    //print_r($array);
    //die;
    $f = fopen('output.csv', 'w');
    
    $firstLineKeys = false;
    foreach ($array["data"] as $line)
    {
    //    echo "<pre>";
    //    print_r($line);
    //    die;
        if (empty($firstLineKeys))
        {
            $firstLineKeys = array_keys($line);
            fputcsv($f, $firstLineKeys);
            $firstLineKeys = array_flip($firstLineKeys);
        }
        $line_array = array($line['reference']);
    
        foreach ($line['images'] as $value)
        {
            array_push($line_array,$value);
        }
        fputcsv($f, $line_array);
    
    }
    echo "Success";
    ?>
    

    【讨论】:

    • 我正在尝试但不导出任何内容,不创建文件 output.csv 我一直在尝试
    • 你能提供我原始的 json 字符串吗?
    • 尝试编辑后的解决方案,这个工作输出文件将是 1.28MB :) 如果工作正常。将此答案标记为已接受
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多