2016-02-07 85 views
1

我有下一個Excel表,其中第一列的日期和其他數據(B,C)中的一些數據。將Excel錶轉換爲2d數組

1.

enter image description here

2.

enter image description here

我想將它轉化爲表像圖三: enter image description here

要做到這一點,你需要在一小時內將每行和每列的數據相加(不要kn例如,從第一張圖片我們得到(0.0022 + 0.0078 + 0.0021 + 0.0078)* 2400≈47 - 從第三張圖片的表格的第一個元素。

我試圖用PHPExcel庫做到這一點:

$objPHPExcel = new PHPExcel(); 
$objPHPExcel = PHPExcel_IOFactory::load("data.xlsx"); 
$highestRow = $objPHPExcel->getActiveSheet()->getHighestRow(); 
$day = 1; 
$hour = 1; 
$data = array(array()); 
for ($i = 1; $i < $highestRow + 1; $i++) { 
    $cell1 = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1, $i)->getValue(); 
    $cell2 = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(2, $i)->getValue(); 
    $data[$day][$hour] += $cell1 + $cell2; 
    if($i % 2 == 0) { $data[$day][$hour] *= 2400; $hour++; } 
    if($i % 48 == 0) $day++; 
    if($hour % 24 == 0) $hour = 1; 
} 

但不是工作的代碼我得到的錯誤 enter image description here

第22行:

$data[$day][$hour] += $cell1 + $cell2; 

我覺得這事與我的二維數組,因爲我可以看到我的數組鍵是不好的(不是1-24小時)。

什麼應該是代碼來獲取我的數據在二維數組像$data[1..29-31][1-24]或任何其他?或者可能從圖片#3以其他方式獲得表格?

+0

* related:* A1:A1487左對齊,A1488右對齊的事實與我有關。單元格中的左對齊通常表示文本;在這種情況下是日期時間的文本表示。如果這些是真實的日期時間,則默認情況下它們全都是右對齊的。 – Jeeped

+0

哪一條是22號線? –

+0

@接受它並不重要。我可以計算像highestRow/48這樣的天數。 –

回答

1

當你的行數22

$data[$day][$hour] += $cell1 + $cell2; 

您正在試圖增加$data[$day][$hour]它甚至還沒有確定。在嘗試增加它之前,您需要定義它。我不確定,它會使工作表像你期待的那樣工作。但是這會解決你所得到的錯誤。 (在22行之前加上)

if(!isset($data[$day])) { 
    $data[$day] = array(); //Setting the day which will get the hours in array 
} 
if(!isset($data[$day][$hour])) { 
    $data[$day][$hour] = 0; //Setting the hours which will contain the incremented values for each hour. Setting it 0, the next code blocks will increment it. 
} 
+0

問題解決了,似乎。通知真的出現了,因爲沒有聲明數組,我在小時檢測中犯了一個小錯誤。如果($ hour%24 == 0)$ hour = 1,而不是 ; 應該是: if($ hour%25 == 0)$ hour = 1; 謝謝@TaReQ MahMooD。 –