我的csv文件中的数据看起来像这样。当我尝试使用下面的代码时,前导零正在删除。没有固定长度的文章编号。它可以是任何长度
000086;Grand Trunk Schlafsack, grün;892902000086;1;34,27
000109;Grand Trunk Hängematte, grün;0892902000109;1;15,57
BD159-150-2XL;Grand Trunk Hängematte, blau;0892902000116;1;15,57
BD240-150-44;Grand Trunk Hängematte, blau;0892902000116;1;15,57
BD235-200-35/38;Grand Trunk Hängematte, blau;0892902000116;1;15,57
下面是我使用phpexcel
将csv导入myssql的代码$objReader = PHPExcel_IOFactory::createReader('CSV');
try {
$inputFileType = 'CSV';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader = $objReader->setDelimiter(";");
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
$NewArray = $objPHPExcel->getActiveSheet()->toArray();
echo '<pre>';
print_r($NewArray);
我从上面的代码中输出的是
[0] => Array
(
[0] => 86
[1] =>
[2] => 892902000086
[3] => 1
[4] => 34,27
)
[1] => Array
(
[0] => 109
[1] =>
[2] => 892902000109
[3] => 1
[4] => 15,57
)
[2] => Array
(
[0] => BD159-150-2XL
[1] =>
[2] => 892902000116
[3] => 1
[4] => 15,57
)
[3] => Array
(
[0] => BD240-150-44
[1] =>
[2] => 892902000116
[3] => 1
[4] => 15,57
)
[4] => Array
(
[0] => BD235-200-35/38
[1] =>
[2] => 892902000116
[3] => 1
[4] => 15,57
)
Here in array[0] leading zeros are removed .But I need same values as in csv like
000086
000109
BD159-150-2XL
BD240-150-44
BD235-200-35/38
How can i achieve this? Threre is no fixed length of article number. It could be 6characters/10 charactes / 3 characters. Any help would be greatly appreciated.
答案 0 :(得分:1)
我猜它会自动将其转换为数字,尝试运行一个简单的循环将其转换回字符串
foreach ($NewArray as &$row) {
$row[0]= sprintf("%06d", $row[0]);
}