2011-05-23 28 views
0

嗨IAM工作,在陣列中的數據有:如何從嵌套的數組做的表格數據上出席報告

Array 
(
    [0] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:09:02 
        [eventID] => OUT 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

    [1] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:09:12 
        [eventID] => IN 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

    [2] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:11:31 
        [eventID] => OUT 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

    [3] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:11:40 
        [eventID] => IN 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

    [4] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:13:17 
        [eventID] => OUT 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

    [5] => Array 
     (
      [ActatekLog] => Array 
       (
        [id] => 1 
        [timeentry] => 2011-02-16 00:13:21 
        [eventID] => IN 
        [terminalSN] => 00111DA08C8B 
        [jpegPhoto] => 
       ) 

     ) 

) 

報告格式是:

Date  In Time  Out Time Work Time 
2011-02-16 

我需要在顯示幫助在時間和在同一天的一排排隊時間。

請幫

+0

我只看到錄入時間。你能解釋一下我們在陣列中看到的更多嗎? – Prikkeldraad 2011-05-23 12:40:10

+0

感謝您的回覆Prikkeldraad,我想以表格形式顯示數據,例如: DATE |在時間| OUT TIME |工作時間 在連續的同一日期的時間和時間 – Tanvier 2011-05-23 12:56:00

+0

當我檢查你的數組我發現所有條目都在同一日期和2011-02-16,並且有三個IN和三個Out。那麼你如何期望你可以根據這些信息獲得一排?請發佈您期望的返回數據。 – Tareq 2011-05-23 17:15:05

回答

0

我使用的數據模型永遠不會改變,並且你的數據已經排序,你想它要排序的方式。因此,對於像這樣的數組:

$data = array 
(
    0 => array 
    (
     'ActateLog' => array 
     (
      'id' => 1, 
      'timeentry' => '2011-02-16 00:09:02', 
      'eventID' => 'OUT', 
      'terminalSN' => '00111DA08C8B', 
      'jpegPhoto' => false 
     ) 
    ), 
    1 => array 
    (
     'ActateLog' => array 
     (
      'id' => 1, 
      'timeentry' => '2011-02-16 00:09:12', 
      'eventID' => 'IN', 
      'terminalSN' => '00111DA08C8B', 
      'jpegPhoto' => false 
     ) 
    ) 
); 

我製成的函數,該函數返回具有n/2行的陣列,並且同時具有IN和OUT同一排。

function processMyArray($data) 
{ 
    $output = array(); 
    $lim = count($data); 
    $i = $j = 0; 

    for($i=0; $i<$lim; $i+=2, $j++) 
    { 
     $output[$j] = array 
     (
      'date' => substr($data[$i]['ActateLog']['timeentry'], 0, 10), 
      'IN' => substr($data[$i]['ActateLog']['timeentry'], 11), 
      'OUT' => substr($data[$i+1]['ActateLog']['timeentry'], 11), 
      'work_time' => strtotime($data[$i+1]['ActateLog']['timeentry']) - strtotime($data[$i]['ActateLog']['timeentry']) 
     ); 
    } 

    return $output; 
} 
+0

謝謝gilden你的解決方案是偉大的,但仍然有一個小故障plz幫助我解決這個問題。我們需要爲2011-05-01〜2011-05-25的所需日期創建出勤報告。我們需要通過一個陣列中的狀態之後的功能processMyArray()被匹配通過日期循環, – Tanvier 2011-05-25 12:31:03

+0

這樣 ' 爲($ I = 0; $ I <$計數; $ I ++) \t {\t \t $ diff3 = daysDifference($ contents [$ i] ['ActatekLog'] ['timeentry'],$ date); \t \t if($ diff3 == 0) \t \t {$ sameDay = $ contents [$ i]; \t \t $ result = processMyArray($ sameDay); \t \t print_r($ result); \t \t} \t \t \t \t} \t \t ' 功能需要ID和我們傳遞陣列解決關鍵[ActatekLog] plz幫助 – Tanvier 2011-05-25 12:38:39

+0

感謝的問題。你是一個搖滾明星 – Tanvier 2011-05-25 14:01:23

0

在這裏你去那會做到這一點,雖然你沒有你的數組中你在你的表格要你說出兩個值。

<table> 
    <tr> 
     <th>Date</th> 
     <th>Time In</th> 
     <th>Time Out</th> 
     <th>Work Time</th> 
    </tr> 
<?php foreach ($thisArr as $arr) : ?> 
    <tr> 
     <td><?php echo date('Y-m-d'); ?></td> 
     <td><?php echo $arr['ActatekLog']['timeentry']; ?></td> 
     <td><?php echo "no time out in the array??"; ?></td> 
     <td><?php echo "you need to calculate this I assume"; ?></td> 
    </tr> 
<?php endforeach; ?> 
</table> 
+0

Prikkeldraad,這個代碼的問題是它不會在一行中顯示時間和時間,而是在一行中顯示時間,在下一行中顯示時間。真的很感謝你的幫助,但我們需要擦亮它,我試圖提取列表中的數據,但沒有用 – Tanvier 2011-05-23 13:24:40

+0

@Tanvier我不是Prikkeldraad並使用你的數組,我發佈的代碼將打印出時間和時間相同行爲嵌套的每一個數組....所以我不明白你在說什麼 – martynthewolf 2011-05-23 13:29:36

1

我會先從格式化輸出數組開始。

$arr = array(); 
foreach ($result as $row) { 
    $arr[ $row[ 'terminalSN' ] ][ $row[ 'eventID' ] ] = $row; 
} 

然後輸出應該更容易一些:

<table> 
    <tr> 
     <th>Date</th> 
     <th>Time In</th> 
     <th>Time Out</th> 
     <th>Work Time</th> 
    </tr> 
    <?php foreach ($arr as $key => $value) : ?> 
     <tr> 
      <td><?php echo date('Y-m-d', strtotime($value[ 'IN' ][ 'timeentry' ])); ?></td> 
      <td><?php echo date('H:i:s', strtotime($value[ 'IN' ][ 'timeentry' ])); ?></td> 
      <td><?php echo date('H:i:s', strtotime($value[ 'OUT' ][ 'timeentry' ])); ?></td> 
      <td><?php echo "Calculate based on out minus in. Pretty simple at this point." ?></td> 
     </tr> 
    <?php endforeach; ?> 
</table>