如何在具有数百条记录的现有csv中添加新列并从数组中填充它?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在具有数百条记录的现有csv中添加新列并从数组中填充它?相关的知识,希望对你有一定的参考价值。
我正在尝试填充这样的csv文件。
existing.csv
header1 header2 header3
record record2 record3
itemXYZ numberXYZ item40
data1 dataABC anydata
etcA etc3 etcX5a
...
总行数= 522
记录和项目可以是任何字符串或数字。
要插入“ newHeader”的数据]]
array (size=522) 0 => string 'Global' (length=6) 1 => string 'Press' (length=6) 2 => string 'Insurance' (length=9) 3 => string 'Presse' (length=6) 4 => string 'Insurance' (length=10) 5 => string 'Global' (length=6) 6 => string 'Assurances' (length=10) 7 => string 'Global' (length=6) 8 => string 'Global' (length=6) 9 => string 'Global' (length=6) 10 => ...
结果应该是这样的:
header1 header2 header3 newHeader record record2 record3 Global itemXYZ numberXYZ item40 Press data1 dataABC anydata Insurance, etcA etc3 etcX5a Presse ...
[我在这里使用另一篇文章的稍加修改的代码(https://stackoverflow.com/a/36143377/11256861)。
foreach ($cat as $value) writeToCsv($value); function writeToCsv($value) $delimiter = ','; //your column separator $csv_data = array(); $row = 1; if (($handle = fopen('input/existing_populated.csv', 'r')) !== false) while (($data = fgetcsv($handle, 1000, $delimiter)) !== false) $csv_data[] = $data; $row++; fclose($handle); $extra_columns = array('columnName' => $value); foreach ($csv_data as $i => $data) if ($i == 0) $csv_data[$i] = array_merge($data, array_keys($extra_columns)); else $csv_data[$i] = array_merge($data, $extra_columns); if (($handle = fopen('output/result.csv', 'w')) !== false) foreach ($csv_data as $data) fputcsv($handle, $data, $delimiter); fclose($handle);
最后,此方法使用从foreach循环读取的最新类别填充所有行。这是显而易见的。但是我不知道如何将每个类别从新列“分配”到现有记录。
谢谢您的帮助。
基于@bestprogrammerintheworld的解决方案
我做了一个小的修改。我的类别数组已经包含标题。我不知道这是否是最佳做法,但是很明显。感谢@bestprogrammerintheworld
$delimiter = ',';
$csv_data = array();
if (($handle = fopen('input/existing_populated.csv', 'r')) !== false)
while (($data = fgetcsv($handle, 1000, $delimiter)) !== false)
$csv_data[] = $data;
fclose($handle);
$row = 0;
foreach ($cat as $index => $item)
if ($row === 0)
$csv_data[$index + $row][] = $item;
else
$csv_data[$index + 1][] = $item;
//Save new array to csv
if (($handle = fopen('output/result.csv', 'w')) !== false)
foreach ($csv_data as $data)
fputcsv($handle, $data);
fclose($handle);
我正在尝试填充这样的csv文件。 exist.csv header1 header2 header3 record record2 record3 itemXYZ numberXYZ item40 data1 dataABC anydata etcA etc3 ...
答案
<?php
$newHeader = array(
'Global',
'Press',
'Insurance',
'Presse',
'Insurance',
'Global',
'Assurances',
'Global'
);
//Load csv file
$delimiter = ';';
$csv_data = array();
$row = 1;
if (($handle = fopen('input/existing_populated.csv', 'r')) !== false)
while (($data = fgetcsv($handle, 1000, $delimiter)) !== false)
$csv_data[] = $data;
fclose($handle);
/*
Now the output of $csv_data looks something like:
Array
(
[0] => Array
(
[0] => header1
[1] => header2
[2] => header3
)
[1] => Array
(
[0] => record
[1] => record2
[2] => record3
)
[2] => Array
(
[0] => itemXYZ
[1] => numberXYZ
[2] => item40
)
[3] => Array
(
[0] => data1
[1] => dataABC
[2] => anydata
*/
//Add header to array
$csv_data[0][] = 'newHeader'; (is [0][3] below)
/*
Array
(
[0] => Array
(
[0] => header1
[1] => header2
[2] => header3
[3] => newHeader
)
*/
//Add data to array ($index is only viable here if $newHeader
//is indexed by 0,1,2 etc)
/* $newHeader = array(
'Global',
'Press'...
makes $newHeader[0] (Global), $newHeader1] Press etc...
*/
foreach($newHeader as $index=>$item)
$csv_data[$index + 1][] = $item;
//Save new array to csv
if (($handle = fopen('output/result.csv', 'w')) !== false)
foreach ($csv_data as $data)
fputcsv($handle, $data);
fclose($handle);
以上是关于如何在具有数百条记录的现有csv中添加新列并从数组中填充它?的主要内容,如果未能解决你的问题,请参考以下文章