为什么获得具有0索引的数组?

时间:2015-05-20 09:06:59

标签: php codeigniter

在我的var_dump之后,我看到这些数组具有相同的索引。为什么会这样?我该如何制作而不是零呢?它将是0,1,2, 3 .... 这是我的完整代码,它是一个非常长的代码:我在codeigniter视图中有这个表。

                Total       Expected
  Day(s)   |Hours Worked | Total hr/day | Undertime | Overtime
2015-05-01 | 09:00:00    | 09:00:00     | 00:00:00  | 00:00:00
2015-05-05 | 08:32:00    | 09:00:00     | 00:28:00  | 00:00:00  
2015-05-06 | 09:21:00    | 09:00:00     | 00:00:00  | 00:21:00
2015-05-07 | 08:22:00    | 09:00:00     | 00:38:00  | 00:00:00  
2015-05-08 | 06:30:00    | 09:00:00     | 00:30:00  | 00:00:00  
2015-05-09 | 08:42:00    | 09:00:00     | 00:18:00  | 00:00:00  
2015-05-10 | 09:00:00    | 09:00:00     | 00:00:00  | 00:00:00
2015-05-11 | 09:24:00    | 09:00:00     | 00:00:00  | 00:24:00
2015-05-12 | 09:41:00    | 09:00:00     | 00:00:00  | 00:41:00
2015-05-13 | 08:46:00    | 09:00:00     | 00:14:00  | 00:00:00
2015-05-14 | 08:54:00    | 09:00:00     | 00:06:00  | 00:00:00
            ttl: 96:12:00|ttl: 99:00:00 |ttl:       |ttl:   

从total_hr / day和total_hours获得总小时数与Undertime和Overtime不同。因为它应该是Undertime(expected_total_hr / day-total_hours_worked)的结果,加班是(total_hours_worked-expected_total_hr / day)

我想要的是得到总体时间和加班费。

foreach ($unique_date as $late) 
         {   
    $ttl_hrs_wrked = $late['total_hrs_worked'];
    $expected_hrsAday = $late['expected_hrsAday'];
    $duty  = strtotime($ttl_hrs_wrked);
    $expected = strtotime($xpected_hrsAday);
    $hour  = $expected-$duty; //getting the Undertime
             //$hour  = $duty-$expected -> getting the Overtime                   

    I want to try the array_sum() that's why i tried making $hour as an array. but when i checked. it resulted to:

    $timeArrays = array( date("00:i",$hour ));
    }

    var_dump($timeArrays);

    ?>

    array (size=1)
      0 => string '00:00' (length=5)
    array (size=1)
      0 => string '00:28' (length=5)
    array (size=1)
      0 => string '00:39' (length=5)
    array (size=1)
      0 => string '00:38' (length=5)
    array (size=1)
      0 => string '00:30' (length=5)
    array (size=1)
      0 => string '00:18' (length=5)
    array (size=1)
      0 => string '00:00' (length=5)
    array (size=1)
      0 => string '00:36' (length=5)
    array (size=1)
      0 => string '00:19' (length=5)
    array (size=1)
      0 => string '00:14' (length=5)
    array (size=1)
      0 => string '00:06' (length=5)

I tried doing this: $timeArrays[] = (date("00:i",$hour ));
   but when i var_dump( $timeArrays);
it resulted into this.

array (size=1)
  0 => string '00:00' (length=5)
array (size=2)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
array (size=3)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
array (size=4)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
array (size=5)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
array (size=6)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
array (size=7)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
  6 => string '00:00' (length=5)
array (size=8)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
  6 => string '00:00' (length=5)
  7 => string '00:36' (length=5)
array (size=9)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
  6 => string '00:00' (length=5)
  7 => string '00:36' (length=5)
  8 => string '00:19' (length=5)
array (size=10)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
  6 => string '00:00' (length=5)
  7 => string '00:36' (length=5)
  8 => string '00:19' (length=5)
  9 => string '00:14' (length=5)
array (size=11)
  0 => string '00:00' (length=5)
  1 => string '00:28' (length=5)
  2 => string '00:39' (length=5)
  3 => string '00:38' (length=5)
  4 => string '00:30' (length=5)
  5 => string '00:18' (length=5)
  6 => string '00:00' (length=5)
  7 => string '00:36' (length=5)
  8 => string '00:19' (length=5)
  9 => string '00:14' (length=5)
  10 => string '00:06' (length=5)

1 个答案:

答案 0 :(得分:1)

这是因为array而发生的。数组存储索引为0的值。删除阵列并将其存储在$timeArrays[]中。试试这个 -

$timeArrays[] = date("00:i",$hour);

希望问题能够得到解决。