dqba94394 2014-02-09 14:32
浏览 21
已采纳

在PHP中求和并合并数组

i have two array like below

Array
(
    [0] => Array
        (
            [taskcount] => 3
            [client_id] => 1
            [client_name] => No Project Set
        )

    [1] => Array
        (
            [taskcount] => 1
            [client_id] => 4
            [client_name] => Check
        )

    [2] => Array
        (
            [taskcount] => 1
            [client_id] => 5
            [client_name] => Others
        )

)

and

Array
    (
        [0] => Array
            (
                [taskcount] => 1
                [client_id] => 5
                [client_name] => Others
            ),
        [1] => Array
            (
                [taskcount] => 1
                [client_id] =>7
                [client_name] => Othersnew
            )

    )

and i want to merge two array so as to return some thing like,means i want to sum the taskcount if both array has common client_id

Array
    (
        [0] => Array
            (
                [taskcount] => 3
                [client_id] => 1
                [client_name] => No Project Set
            )

        [1] => Array
            (
                [taskcount] => 1
                [client_id] => 4
                [client_name] => Check
            )

        [2] => Array
            (
                [taskcount] => 2
                [client_id] => 5
                [client_name] => Others
            )
        [3] => Array
            (
                [taskcount] => 1
                [client_id] =>7
                [client_name] => Othersnew
            )


    )
  • 写回答

2条回答 默认 最新

  • donglie1994 2014-02-09 14:59
    关注

    This works(Albeit being the hard way) -

    function merge_n_add($a1, $a2){
        $result = $a1;
        $client_ids = array_map(function($a){return $a['client_id'];}, $result);
        foreach($a2 as $v){
            if(in_array($v['client_id'], $client_ids)){
                $res_index = array_search($v['client_id'] ,$client_ids);
                $result[$res_index]['taskcount'] += $v['taskcount'];
            }else{
                $result[] = $v;
            }
        }
        return $result;
    }
    //Assuming the 2 arrays are $a1 and $a2
    var_dump(merge_n_add($a1, $a2));
    

    This is the output for the given input -

    /*
        OUTPUT
    */
    array
      0 => 
        array
          'taskcount' => int 3
          'client_id' => int 1
          'client_name' => string 'No Project Set' (length=14)
      1 => 
        array
          'taskcount' => int 1
          'client_id' => int 4
          'client_name' => string 'Check' (length=5)
      2 => 
        array
          'taskcount' => int 2
          'client_id' => int 5
          'client_name' => string 'Others' (length=6)
      3 => 
        array
          'taskcount' => int 1
          'client_id' => int 7
          'client_name' => string 'Othersnew' (length=9)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看