0

我想用标题作为键的 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"]=> string(5) "Harry" ["description"]=> string(4) "test" ["offer_url"]=> string(42) "demo.com/admin /offers/add" ["preview_url"]=> string(42) "demo.com/admin/offers/add" ["expiration_date"]=> string(9) "23-8-2013" }

array(5) { ["name"]=> string(5) "Gerry" ["description"]=> string(4) "test" ["offer_url"]=> string(42) "demo.com/admin /offers/add" ["preview_url"]=> string(42) "demo.com/admin/offers/add" ["expiration_date"]=> string(9) "23-8-2013" }

array(5) { ["name"]=> string(5) "merry" ["description"]=> string(4) "test" ["offer_url"]=> string(42) "demo.com/admin /offers/add" ["preview_url"]=> string(42) "demo.com/admin/offers/add" ["expiration_date"]=> string(9) "23-8-2013" }

4

1 回答 1

0

您必须通过多维数组并向组合参数添加额外的索引,如下所示 - combine[]=...:

            // create a new array with the elements in $head as keys
            // and elements in array $data as values.
            $combined[] = array_combine($head,$data);
于 2013-02-27T14:31:55.407 回答