PHP:按特定键分组(多维,关联)数组和求和值

时间:2014-12-11 16:00:34

标签: php arrays multidimensional-array

我已经用group / sum数组值搜索了很多例子 - 遗憾的是我无法解决我的问题。 我有数组$ data,看起来像这样:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2014
            [context] => 'aaa'
        )

    [1] => Array
        (
            [0] => 12
            [1] => 2014
            [context] => 'aaa'
        )       

    [2] => Array
        (
            [0] => 5
            [1] => 2014
            [context] => 'zzz'
        )               
)

我想通过' context'对其值(但不是全部)进行分组和汇总。

所以期望的输出是:

Array
(
    [0] => Array
        (
            [0] => 13
            [1] => 2014
            [context] => 'aaa'
        )

    [1] => Array
        (
            [0] => 5
            [1] => 2014
            [context] => 'zzz'
        )       

)

我远离这个预期的输出。我尝试过类似的事情:

$result = array();
foreach ($data as $subArray) 
{
  foreach ($subArray as $row)
  {
    $result[$row['context']] = $row['context'];
    $result[$row['1']] = $row['1'];
    $result[$row['0']] += $row['0'];
  }
}

但当然它不起作用而且我没有想法。你能给我一个提示吗?我还能尝试什么?

2 个答案:

答案 0 :(得分:1)

您可以使用临时数组(此处为$newArr)完成此操作。可以试试这样的东西

$newArr = array();
foreach($your_arr as $key=>$val){
    $index = $val['context'].$val[1];
    if(isset($newArr[$index])){
        $val_0 = $newArr[$val['context'].$val[1]][0] + $val[0];
        $newArr[$val['context'].$val[1]] = array($val_0, $val[1], 'context'=>$val['context']);
    }else{
        $newArr[$val['context'].$val[1]] = $val;
    }
}
$result = array_values($newArr);
print '<pre>';
print_r($result);
print '</pre>';

答案 1 :(得分:1)

问题是你在覆盖循环中的元素并且指望额外的嵌套级别:

$data = array(
    0 => array(
            0 => 1,
            1 => 2014,
            'context' => 'aaa'
        ),

    1 => array(
            0 => 12,
            1 => 2014,
            'context' => 'aaa'
        ),       

    2 => array(
            0 => 5,
            1 => 2014,
            'context' => 'zzz'
        )               
);

$result = array();

// the elements to sum - since everything is mixed together.
// the values in this array should be the index you want to sum
// ex. $sum = array(2,3) would sum $data[2] and $data[3]
$sum = array(0);

foreach ($data as $subArray) 
{
  $context = $subArray['context'];

  if (!isset($result[$context])) {
    // initialize the result for this context because it doesnt exist yet
    $result[$context] = array();
  }

  // you had an extra nesting level here
  // $row was equiv to 'aaa' or '2014' whereas you thought it was
  // array('context' => 'aaa', 0 => 5, 1 => '2014')
  foreach ($subArray as $idx => $val)
  {
    // you were also constantly overrwriting $result'aaa'] (or whatever context) here
    if (in_array($idx, $sum)) {
       $result[$context][$idx] = isset($result[$context][$idx])
         ? $result[$context][$idx] + $val
         : $val;
    } else {
      // this will keep overwriting anything that isnt in the $sum array
      // but thats ok because those values should be the same with, or the last
      // one should win. If you need different logic than that then adjsut as necessary
      $result[$context][$idx] = $val;
    }
  }
}

printf('<pre>%s</pre>', print_r($result, true));