我想用csv文件制作一个数组,标题为关键字。但是当我将脚本插入到数据库中时,它会覆盖csv文件的前一行,并且只有最后一个值存储在数据库中。
我认为是因为数组的默认值为5.如何更改该值以便创建新值?
这是脚本
<?php
// open the file.
if (($handle = fopen("test2.csv", "r")) !== FALSE) {
// read the column headers in an array.
$head = fgetcsv($handle, 1000, ";");
// read the actual data.
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
// create a new array with the elements in $head as keys
// and elements in array $data as values.
$combined = array_combine($head,$data);
// print.
var_dump($combined);
}
// done using the file..close it,
}
?>
这是输出
array(5){[“name”] =&gt; string(5)“Harry”[“description”] =&gt; string(4)“test”[“offer_url”] =&gt; string(42)“demo.com/admin/offers/add”[“preview_url”] =&gt; string(42)“demo.com/admin/offers/add”[“expiration_date”] =&gt; string(9)“23-8-2013”}
array(5){[“name”] =&gt; string(5)“Gerry”[“description”] =&gt; string(4)“test”[“offer_url”] =&gt; string(42)“demo.com/admin/offers/add”[“preview_url”] =&gt; string(42)“demo.com/admin/offers/add”[“expiration_date”] =&gt; string(9)“23-8-2013”}
array(5){[“name”] =&gt; string(5)“merry”[“description”] =&gt; string(4)“test”[“offer_url”] =&gt; string(42)“demo.com/admin/offers/add”[“preview_url”] =&gt; string(42)“demo.com/admin/offers/add”[“expiration_date”] =&gt; string(9)“23-8-2013”}
答案 0 :(得分:0)
你必须通过多维数组并向组合参数添加额外的索引,如下所示 - 合并[] = ...:
// create a new array with the elements in $head as keys
// and elements in array $data as values.
$combined[] = array_combine($head,$data);