douhuo0884 2018-08-17 08:57
浏览 55
已采纳

想在php中添加多维数组

I have two multi dimensional array.If the key is same then i want to get the sum how do i do that. one is-

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
        )

)

2nd one is- one is-

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
        )


)

so my result would be

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

Code is

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条回答 默认 最新

  • dream198731 2018-08-17 09:12
    关注

    Here is your code,

    <?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;
    

    Here is your working code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 将USDZ文件转化为带颜色的OBJ文件
  • ¥15 对象代号: , 表单: 不存在!
  • ¥15 WebSocket的问题
  • ¥15 centos上启动kylin后网页报错404
  • ¥20 使用hackrf进行信号收发时接收到的信号幅度太小
  • ¥15 WebSocket的问题
  • ¥15 BDSBAS-B1C和B1C信号有什么不同
  • ¥15 在半圆平面内随机生成点坐标
  • ¥15 系统容量变化的几种多址方式TDMA, CDMA,FDMA,OFDMA 对比,应该给的是一个曲线 图,随着系统容量的增加,几种多址方式性能的对比 图,MATLAB程序仿真折线图
  • ¥15 用visual Studio 写c ++只运行上一个旧代码的运行结果是怎么回事