合并具有相同键索引的数组取决于相同键的数量

时间:2014-06-13 02:26:24

标签: php arrays multidimensional-array foreach key

$groupLevelOne = array();
foreach ($data['display'] as $item) {
  $key = implode($data['data'][$i]['eid'])
  .date('Y-m',strtotime($item['date_d'][0]));
  if (!isset($groupLevelOne[$key])) {
    $groupLevelOne[$key] = array(
      'employeeId' => $data['data'][$i]['eid'],
      'efullname' => $item['fullname'][0],
      'date_d' => date('Y-m',strtotime($item['date_d'][0])),
      'hrsdiff' => $item['hrsdiff'][0],
      'dayspresent' => $item['key'][0],
    );
  } 
  else {
    $groupLevelOne[$key]['dayspresent'] = $groupLevelOne[$key]['dayspresent'] + $item['key'][0];
  }
  $i++;
}

已经有此代码显示此代码,它们是具有相同ID和日期的组

Array
(
    [12014-01] => Array
        (
            [employeeId] => Array
                (
                    [0] => 1
                )

            [efullname] => IBARDOLAZA VIRGILIO
            [date_d] => 2014-01
            [hrsdiff] => 5.0333
            [dayspresent] => 2
        )

    [12014-02] => Array
        (
            [employeeId] => Array
                (
                    [0] => 1
                )

            [efullname] => IBARDOLAZA VIRGILIO
            [date_d] => 2014-02
            [hrsdiff] => 8.0333
            [dayspresent] => 24
        )

    [12014-03] => Array
        (
            [employeeId] => Array
                (
                    [0] => 1
                )

            [efullname] => IBARDOLAZA VIRGILIO
            [date_d] => 2014-03
            [hrsdiff] => 8.0667
            [dayspresent] => 26
        )

   )

让我的下一级操作分组与同一个employeeID和不同的日期同义词合并,这将是1个看起来像这样的数组

[1] => Array
      (
         [employeeId] => Array
           (
              [0] => 1
           )

           [efullname] => IBARDOLAZA VIRGILIO
           [date_d1] =>2014-01
           [dayspresent1] => 2
           [date_d2] =>2014-02
           [dayspresent2] => 24
           [date_d3] =>2014-03
           [dayspresent3] => 26
     )

------------跟进问题-------------

[0] => Array
        (
            [employeeId] => 21
            [efullname] => MANOGURA EDGAR
            [hrsdiff] => 2.2331
            [days] => Array
                (
                    [2014-02] => 23
                    [2014-03] => 26
                    [2014-04] => 23
                )

        )

[1] => Array
        (
            [employeeId] => 1
            [efullname] => IBARDOLAZA VIRGILIO
            [hrsdiff] => 5.0333
            [days] => Array
                (
                    [2014-01] => 2
                    [2014-02] => 24
                    [2014-03] => 26
                    [2014-04] => 26
                )

        )

我希望我的第一个索引有[2014-01] => 0 所以他们将拥有所有相同的日子索引

1 个答案:

答案 0 :(得分:0)

您可以在一次运行中完成所有这些操作:

$groups = array();
foreach ($data['display'] as $item) {
    $eid = implode($data['data'][$i]['eid']);
    $date = date('Y-m',strtotime($item['date_d'][0]));

    if (!isset($groups[$eid])) {
        $groups[$eid] = array(
            'employeeId' => $eid,
            'efullname' => $item['fullname'][0],
            'hrsdiff' => $item['hrsdiff'][0],
            'days' => array(
                $date => $item['key'][0],
            )
        );
    } else {
        @$groups[$eid]['days'][$date] += $item['key'][0];
    }
}