我需要一些帮助,因为我不明白。我正在使用PHPExcel读取XLS文件,如下所示:
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = 'precios.xls';
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$array_data = array();
foreach ($rowIterator as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$rowIndex = $row->getRowIndex();
$array_data[$rowIndex] = array('A' => '', 'B' => '', 'C' => '', 'D' => '', 'E' => '', 'F' => '', 'G' => '');
foreach ($cellIterator as $cell) {
if ('A' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('B' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('C' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('D' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('E' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('F' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
} else if ('G' == $cell->getColumn()) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
}
echo '<pre>';
print_r($array_data);
返回类似的内容:
Array
(
[1] => Array
(
[A] => ALGARROBO
[B] => 10800
[C] => 10800
[D] => 16450
[E] => 19150
[F] => 23100
[G] => 23100
)
[2] => Array
(
[A] => ALTO HOSPICIO
[B] => 27950
[C] => 27950
[D] => 39950
[E] => 43900
[F] => 55550
[G] => 55550
)
[3] => Array
(
[A] => ANCUD
[B] => 7000
[C] => 7000
[D] => 10700
[E] => 13250
[F] => 15800
[G] => 15800
)
[4] => Array
(
[A] => ANDACOLLO
[B] => 12950
[C] => 12950
[D] => 18850
[E] => 21650
[F] => 26500
[G] => 26500
)
);
我需要做的就是从这里开始,不要想到将结果转化为:
1:10.800,6:10.800,12:16.450,18:19.150,24:23.100,30:23.100 // this is an example for first array
1:B,6:C,12:D,18:E,24:F,30:G // the same thing using arrays keys
还要注意数字“。”对于价值观,可以给我一些想法或一些示例代码来完成这项工作吗?
答案 0 :(得分:2)
首先要做的事情。在if / else代码块中,所有语句都是相同的。如果您不打算在每种情况下做任何不同的事情,那么您应该将代码更改为
foreach ($cellIterator as $cell) {
$array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
}
关于你的问题,我将使用for counter
遍历数组$str = "";
for ($i = 0; $i < count($arr); $i++) {
if ($i > 1) {
$str .= ($i-1)*6;
} else {
$str .= $i;
}
$str .= .":". substr($arr, 0, 2) . "." . substr($arr, 3);
}