繁体   English   中英

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

[英]PHP: Group (multidimensional, associative) array and sum values by specific key

我搜索了很多带有组/和数组值的示例-不幸的是,我无法解决问题。 我得到的数组$ 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'
        )               
)

我想按“上下文”分组并汇总其值(但不是全部)。

因此,期望的输出是:

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'];
  }
}

但是,当然这是行不通的,我没有想法。 你能给我一个提示吗? 我还能尝试什么?

您可以使用临时数组(此处$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>';

问题是您正在覆盖循环中的元素,并且您还指望一个额外的嵌套级别:

$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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM