【发布时间】:2017-04-25 12:44:43
【问题描述】:
我正在从 CSV 文件中获取数据并将其添加到数组中,以便将其保存到我的数据库中。这是数组格式:
Array
(
[0] => Array
(
[0] => Product code
[1] => Product text
[2] => Stockcode
[3] => Origin
[4] => Batchnumber
[5] => Quantity
)
[1] => Array
(
[0] => 02-018
[1] => TEMPCELL13 TIF 120 HOUR 6P/P
[2] => OK1
[3] =>
[4] =>
[5] => 13
)
[2] => Array
(
[0] => 02-038
[1] => TEMPCELL60 TIF 120 HOUR(BROWN)
[2] => OK1
[3] =>
[4] =>
[5] => 15
)
)
其中,$data[0] 与我数据库中的列同名:
所以,我想遍历所有数组,但跳过$data[0],并将其添加到我的数据库中。这是我目前拥有的:
function process_csv($file) {
$file = fopen($file, "r");
$data = array();
while (!feof($file)) {
$data[] = fgetcsv($file,null,';');
}
fclose($file);
unset($data[0]); //Unset the header information, since we only want the values to parse into our inventory database. $data[0] contains the header from the CSV = columns in our database
foreach($data as $insert){
//Insert $data to my database
// $insert[] now holds the array
}
}
循环遍历每个数组并添加它的最佳方法是什么?此外,如果“product_code”已经存在于数据库中,那么它应该只是UPDATE该行。
【问题讨论】: