【问题标题】:How to create the CSV with headers so it can be imported to Excel如何创建带有标题的 CSV,以便可以将其导入 Excel
【发布时间】:2019-11-18 23:25:06
【问题描述】:

我正在尝试使用此 api 并导出 JSON 结果,以便可以将其导入 Excel。

我已经尝试了下面的代码,但标题是垂直存储的,而不是在文件顶部然后打印下面的值。文件中有多个关键字,所以不只是查询一个。


$apikey = 'XXXXXXXXXXXX';

$array = explode("\n", file_get_contents('kw1.txt'));

$params = [
  'apikey' => $apikey,
  'keyword' => $array,
  'metrics_location' => '2840',
  'metrics_language' => 'en',
  'metrics_network' => 'googlesearchnetwork',
  'metrics_currency' => 'USD',
  'output' => 'json',
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.keywordtool.io/v2/search/volume/google');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$response = json_decode($output, TRUE);

//Decode the JSON and convert it into an associative array.
$jsonDecoded = $response;

//Give our CSV file a name.
$csvFileName = 'example.csv';

$fileContent=[];

foreach ($response as  $key =>$value1){
    foreach ($value1 as $key2 => $value2){
           foreach ($value2 as $key3 => $value3){
                $fileContent[]=[$key2, $key3, $value3];
           }
    }
}


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

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

fclose($fp);

结果是

cat,string,cat
cat,volume,6120000
cat,m1,7480000
cat,m1_month,5
cat,m1_year,2019
cat,m2,6120000
cat,m2_month,4
cat,m2_year,2019
cat,m3,6120000
cat,m3_month,3
cat,m3_year,2019
cat,m4,6120000
cat,m4_month,2
cat,m4_year,2019
cat,m5,6120000
cat,m5_month,1
cat,m5_year,2019
cat,m6,6120000
cat,m6_month,12
cat,m6_year,2018
cat,m7,6120000
cat,m7_month,11
cat,m7_year,2018
cat,m8,6120000
cat,m8_month,10
cat,m8_year,2018
cat,m9,6120000
cat,m9_month,9
cat,m9_year,2018
cat,m10,6120000
cat,m10_month,8
cat,m10_year,2018
cat,m11,6120000
cat,m11_month,7
cat,m11_year,2018
cat,m12,6120000
cat,m12_month,6
cat,m12_year,2018
cat,cpc,0.270114
cat,cmp,0.029219098
dog,string,dog
dog,volume,7480000
dog,m1,7480000
dog,m1_month,5
dog,m1_year,2019
dog,m2,7480000
dog,m2_month,4
dog,m2_year,2019
dog,m3,7480000
dog,m3_month,3
dog,m3_year,2019

我希望看到的是:

keyword,volume,m1,m1_month,m1_year...
cat,6120000,7480000,5,2019...
dog,......

【问题讨论】:

    标签: php arrays csv export


    【解决方案1】:

    替换

    $fileContent=[]; 
    

    $fileContent[] = ['keyword','m1_month','m1_year'];
    

    【讨论】:

    • 我已经添加了$fileContent[] = ['keyword','m1_month','m1_year','m2','m2_month','m2_year','m3','m3_month','m3_year','m4','m4_month','m4_year','m5','m5_month','m5_year','m6','m6_month','m6_year','m7','m7_month','m7_year','m8','m8_month','m8_year','m9','m9_month','m9_year','m10','m10_month','m10_year','m11','m11_month','m11_year','m12','m12_month','m12_year']; 但是我如何获取要打印的值?
    • @GrayRidley inside foreach 循环您必须按照与标题相同的顺序将元素放入数组中,即应按照元素的顺序修改此行$fileContent[]=[$key2, $key3, $value3];
    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 2019-12-20
    • 2012-03-11
    • 2010-11-10
    • 2019-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多