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


    )

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)

name them as $Array1, $Array2.

Add one client from $Array2 to $Array1 at a time.

check if same client do sum, otherwise add new.

while (count($Array2) !== 0) {
   $anotherClient = $array_pop($Array2);
   foreach($Array1 as $client) {
       if ($anotherClient['client_id'] === $client['client_id']) {
           $client['taskcount'] += $anotherClient['taskcount'];
           continue;
       } 
   }
   array_push($Array1, $anotherClient);
}