0

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
  • Sounds like a fun recursion problem, it'd be great to see your first attempt. Sorry, mandatory* to see your first attempt, aside from your desired results Commented Feb 9, 2014 at 14:40
  • You have to use array_uintersect() (php.net/manual/en/function.array-uintersect.php) in conjunction with array_merge(). Commented Feb 9, 2014 at 14:41

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.