-1

我有两个多维数组。如果键相同,那么我想得到总和,我该怎么做。一个是——

Array
(
    [2018-08-02] => Array
        (
            [male] => 1
            [female] => 0
        )



    [2018-08-07] => Array
        (
            [male] => 1
            [female] => 0
        )

   [2018-08-09] => Array
        (
            [male] => 1
            [female] => 5
        )

)

第二个是-一个是-

Array
(
    [2018-08-02] => Array
        (
            [male] => 3
            [female] => 4
        )



    [2018-08-07] => Array
        (
            [male] => 1
            [female] => 5
        )
[2018-08-06] => Array
        (
            [male] => 2
            [female] => 3
        )


)

所以我的结果是

Array
(
    [2018-08-02] => Array
        (
            [male] => 4
            [female] => 4
        )



    [2018-08-07] => Array
        (
            [male] => 2
            [female] => 5
        )

   [2018-08-09] => Array
        (
            [male] => 1
            [female] => 5
        )
[2018-08-06] => Array
        (
            [male] => 2
            [female] => 3
        )
)

代码是

foreach ($reportlist as $reportlists){ 
    $daterep=$reportlists['act_date']; 
    $arr[$daterep]['male']=$reportlists['male_cnt']; 
    $arr[$daterep]['female']=$reportlists['female_cnt']; 
}

foreach ($ureportlist as $ureportlists){ 
    $daterep=$rueportlists['act_date']; 
    $arr2[$daterep]['male']=$reportlists['male_cnt']; 
    $arr2[$daterep]['female']=$reportlists['female_cnt'];
}
4

4 回答 4

2

这是你的代码,

<?php
function pr($arr = [])
{
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}
$arr1 = array
    (
    "2018-08-02" => array
    (
        "male"   => 1,
        "female" => 0,
    ),

    "2018-08-07" => array
    (
        "male"   => 1,
        "female" => 0,
    ),

    "2018-08-09" => array
    (
        "male"   => 1,
        "female" => 5,
    ),

);
$arr2 = array
    (
    "2018-08-02" => array
    (
        "male"   => 3,
        "female" => 4,
    ),

    "2018-08-07" => array
    (
        "male"   => 1,
        "female" => 5,
    ),
    "2018-08-06" => array
    (
        "male"   => 2,
        "female" => 3,
    ),
);

function custom_function($arr){
    $retArr = array();
    foreach ($arr as $child) { // arr1, arr2
        foreach ($child as $key => $value) { // traversing through keys
            foreach($value as $k => $v){
                if (isset($retArr[$key][$k])) { // if key is set then add
                    $retArr[$key][$k] += $v;
                } else {
                    $retArr[$key][$k] = $v; // else initiate
                }

            }
        }
    } 
    return $retArr;
}
$result=custom_function(array($arr1,$arr2));
pr($result);die;

这是您的工作代码

于 2018-08-17T09:12:31.763 回答
0

您可以使用 array_intersect_key() 检查是否有匹配的结果来添加它们。例如,假设您的两个数组是 $array1 和 $array2。然后你可以使用

$result = array_intersect_key($array1, $array2);

这将返回数组中的交集。因此,您可以确定需要添加哪些键(在您的情况下,哪些日期)。

于 2018-08-17T09:13:45.133 回答
0

您可以使用array_merge_recursive加入数组,然后对结果数组中的子数组求和

    $res = array_merge_recursive($arr1, $arr2);
    foreach($res as &$date) {
      foreach($date as &$sex) { 
        $sex = array_sum((array) $sex);
      }
    }
    print_r($res);

演示

于 2018-08-17T10:25:16.463 回答
0

您只需要迭代第二个数组并确定日期键是否存在于第一个数组中。 isset()是运行该检查的最佳/最有效方式。

如果第一个数组中不存在第二个数组中的日期,您可以简单地将行的数据存储到第一个数组中。如果日期已经存在于第一个数组中,那么您将需要(单独)添加男性和女性值。

代码:(演示

$array1 = [ 
   '2018-08-02' =>  ['male' => 1, 'female' => 0],
   '2018-08-07' =>  ['male' => 1, 'female' => 0],
   '2018-08-09' =>  ['male' => 1, 'female' => 5]
];
$array2 = [ 
   '2018-08-02' =>  ['male' => 3, 'female' => 4],
   '2018-08-07' =>  ['male' => 1, 'female' => 5],
   '2018-08-06' =>  ['male' => 2, 'female' => 3]
];

foreach ($array2 as $date => $row) {
    if (!isset($array1[$date])) {
        $array1[$date] = $row;                        // store row data with unique date
    } else {
        $array1[$date]['male'] += $row['male'];       // perform addition
        $array1[$date]['female'] += $row['female'];   // perform addition
    }
}
// ksort($array1); if you want to order by date
var_export($array1);

输出:

array (
  '2018-08-02' => 
  array (
    'male' => 4,
    'female' => 4,
  ),
  '2018-08-07' => 
  array (
    'male' => 2,
    'female' => 5,
  ),
  '2018-08-09' => 
  array (
    'male' => 1,
    'female' => 5,
  ),
  '2018-08-06' => 
  array (
    'male' => 2,
    'female' => 3,
  ),
)
于 2018-08-25T07:23:32.217 回答