PHP中多维数组中两个匹配键的值

时间:2016-02-23 15:41:34

标签: php arrays date multidimensional-array sum

我有一个多维数组,有一个日期,名字和&标记。

  0 => 
array (size=3)
  'saledate' => string '2016-02-01' (length=10)
  'name' => string 'John Doe' (length=8)
  'markup' => float 561
1 => 
array (size=3)
  'saledate' => string '2016-02-01' (length=10)
  'name' => string 'John Doe' (length=8)
  'markup' => float 681
2 => 
array (size=3)
  'saledate' => string '2016-02-02' (length=10)
  'name' => string 'John Doe' (length=8)
  'markup' => float 379
3 => 
array (size=3)
  'saledate' => string '2016-02-01' (length=10)
  'name' => string 'Jane Doe' (length=10)
  'markup' => float 205
4 => 
array (size=3)
  'saledate' => string '2016-02-02' (length=10)
  'name' => string 'Jane Doe' (length=8)
  'markup' => int 900
5 => 
array (size=3)
  'saledate' => string '2016-02-02' (length=10)
  'name' => string 'Jane Doe' (length=8)
  'markup' => float 787
 6 => 
array (size=3)
  'saledate' => string '2016-02-03' (length=10)
  'name' => string 'Jane Doe' (length=8)
  'markup' => float 211

我试图按日期对每个人的值求和所需的输出:

 0 => 
array (size=3)
  'name' => string 'John Doe' (length=8)
  '2016-02-01' => float 1242
  '2016-02-02' => float 379
 1 => 
array (size=3)
  'name' => string 'Jane Doe' (length=8)
   '2016-02-01' => float 205
   '2016-02-02' => float 1687
   '2016-02-03' => float 211

这是我所拥有的,但我认为我只是让自己感到困惑......

 $out = array();
 foreach ($in as $row)
 {
   $result[$row['saledate']]['saledate'] = $row['saledate'];
   $result[$row['name']]['name'] = $row['name'];
   $result[$row['date']]['markup'] += $row['markup'];
 }
 $out = array_values($out);

2 个答案:

答案 0 :(得分:0)

name存储为输出数组中的键,它会使结果分组更容易:

$out = array();
foreach ($in as $row) {

  /* set the key using the name, eg. $out['John Doe'] */
  if (! isset($out[$row['name']])) {
    $out[$row['name']] = array('name' => $row['name']); 
  }

  /* if the saledate isn't already exist, set it */
  if (! isset($out[$row['name']][$row['saledate']])) {
    $out[$row['name']][$row['saledate']] = 0;    
  }

  /* add the markup */
  $out[$row['name']][$row['saledate']] += $row['markup'];

}

哪会给你:

Array
(
    [John Doe] => Array
        (
            [name] => John Doe
            [2016-02-01] => 1242
            [2016-02-02] => 379
        )

    [Jane Doe] => Array
        (
            [name] => Jane Doe
            [2016-02-01] => 205
            [2016-02-02] => 1687
            [2016-02-03] => 211
        )

)

如果您不想使用name密钥,请使用$out = array_values($out)

答案 1 :(得分:0)

试试这段代码:

$result = array();

array_walk
(
    $array, 
    function( $row, $result ) use ( $in, &$result )
    {
        $key = array_search( $row['name'], array_column( $result, 'name' ) );
        if( $key === False )
        { $key = array_push( $result, array( 'name'=>$row['name'] ) )-1; }
        $date = $row['saledate'];
        if( !isset( $result[$key][$date] ) ) $result[$key][$date]  = $row['markup'];
        else                                 $result[$key][$date] += $row['markup'];
    }
    , $result
);

print_r( $result );

eval.in demo

在初始化空数组之后,我们通过引用将它作为第二个参数传递给array_walk:在被调用函数内,我们检查名称和日期是否已经存在:如果是,我们添加当前值,否则我们创建具有当前值的新元素。