dtxpz8785 2017-04-11 16:30
浏览 50
已采纳

合并多维数组然后求和值

I have an array with dinamic value like this. the goal is merge array with have same code and sum of value with same code.

$array = array(
    array(
        array(
            'code'=>'AA',
            'name'=>'A Name',
            'cost'=>'10',
        ),
        array(
            'code'=>'AB',
            'name'=>'B Name',
            'cost'=>'15',
        ),
    ),
    array(
        array(
            'code'=>'AA',
            'name'=>'A Name',
            'cost'=>'15',
        ),
        array(
            'code'=>'AB',
            'name'=>'B Name',
            'cost'=>'10',
        ),
        array(
            'code'=>'AC',
            'name'=>'C Name',
            'cost'=>'10',
        ),
    ),
    array(
        array(
            'code'=>'AA',
            'name'=>'A Name',
            'cost'=>'5',
        ),
        array(
            'code'=>'AB',
            'name'=>'B Name',
            'cost'=>'10',
        ),
        array(
            'code'=>'AC',
            'name'=>'C Name',
            'cost'=>'15',
        ),
    ),
);

And i want result like this

$result = array(
    array(
        array(
            'code'=>'AA',
            'name'=>'A Name',
            'cost'=>'30',
        ),
        array(
            'code'=>'AB',
            'name'=>'B Name',
            'cost'=>'35',
        ),
    ),
);

The result is only code AA and BB because code AC not exist on first index. then code the same value to be sum.

Thanks

  • 写回答

5条回答 默认 最新

  • douhong9210 2017-04-12 03:09
    关注

    Demo

    $array = array(
                array(
                    array(
                        'code'=>'AA',
                        'name'=>'A Name',
                        'cost'=>'10',
                    ),
                    array(
                        'code'=>'AB',
                        'name'=>'B Name',
                        'cost'=>'15',
                    ),
                ),
                array(
                    array(
                        'code'=>'AA',
                        'name'=>'A Name',
                        'cost'=>'15',
                    ),
                    array(
                        'code'=>'AB',
                        'name'=>'B Name',
                        'cost'=>'10',
                    ),
                    array(
                        'code'=>'AC',
                        'name'=>'C Name',
                        'cost'=>'10',
                    ),
                ),
                array(
                    array(
                        'code'=>'AA',
                        'name'=>'A Name',
                        'cost'=>'5',
                    ),
                    array(
                        'code'=>'AB',
                        'name'=>'B Name',
                        'cost'=>'10',
                    ),
                    array(
                        'code'=>'AC',
                        'name'=>'C Name',
                        'cost'=>'15',
                    ),
                ),
            );      
    
            $the_biggest_number=count($array);
    
            $new_array = array();
            foreach ($array as $key => $val) {
    
                foreach ($val as $sub_key => $sub_val) {
                    if(!isset($new_array[$sub_val['code']])){
                            $sub_val['count']=1;
                            $new_array[$sub_val['code']]=$sub_val;
                    }
                    else{
                        $new_array[$sub_val['code']]['cost'] += $sub_val['cost'];
                        $new_array[$sub_val['code']]['count'] += 1;
                    }
                }
            }        
    
            $result=array();
            foreach ($new_array as $key => $val) {
                if($val['count']==$the_biggest_number){
                    unset($val['count']);
                    $result[]=$val;
                }
            }
    
            $result= array($result);
    
            echo '<pre>';
            print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?