0

我在这里需要一些帮助,因为我不明白。我正在使用 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

还要注意数字“。” 对于价值观,任何人都可以给我一些想法或一些示例代码来完成这项工作吗?

4

1 回答 1

2

第一件事。在 if/else 代码块中,所有语句都是相同的。如果您不打算在每种情况下做任何不同的事情,那么您应该将代码更改为类似

foreach ($cellIterator as $cell) {
    $array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();        
}

至于您的问题,我将使用 for 计数器遍历数组

$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);
}
于 2013-10-22T16:21:17.727 回答