2

I can't get suitable title for this thread (help me). I can't describe this problem so here the example of my problem.

My array :

Array ( [0] => Array ( [answer] => a [score] => 3 )
[1] => Array ([answer] => b [score] => 4 ) 
[2] => Array ( [answer] => h [score] => 3) 
[3] => Array ( [answer] => a [score] => 4 ))
...

And I wanna get an output like this :

Array ( [0] => Array ( [answer] => a [score] => 7 )
[1] => Array ([answer] => b [score] => 4 ) 
[2] => Array ( [answer] => h [score] => 3))
...

You can see a change of score subkey in index key 0. This is happen because there is two value 'a' in answer subkey from index key 0 and 3. The score changed to 7 because of the sum of both (3+4). Really I don't have an idea for this, sorry for my english and thanks for help. Feel free to comment. :)

2
  • Is the array originating from a database? Commented Mar 26, 2013 at 8:20
  • nope, defined by myself Commented Mar 26, 2013 at 9:12

3 Answers 3

3
$merged = array();

foreach ($array as $answer) {
    if (isset($merged[$answer['answer']])) {
        $merged[$answer['answer']]['score'] += $answer['score'];
    } else {
        $merged[$answer['answer']] = $answer;
    }
}

var_dump($merged);
Sign up to request clarification or add additional context in comments.

Comments

1

Check this answer, not using loop :

$arr  = array ( array ( 'answer' => 'a', 'score' => 3 ),
                array ( 'answer' => 'b', 'score' => 4 ), 
                array ( 'answer' => 'h', 'score' => 3), 
                array ( 'answer' => 'a', 'score' => 4 ));

$t = array_reduce($arr, function($result, $item) {
        if(array_key_exists($item['answer'],$result)){
           $result[$item['answer']]    = array('answer' => $item['answer'], 'score' => $item['score']+$result[$item['answer']]['score']);
        }
        else{
           $result[$item['answer']]    = array('answer' => $item['answer'], 'score' => $item['score']);
        }
    return $result;
},array()); 

echo "<pre>";
print_r($t);

Output :

Array
(
    [a] => Array
        (
            [answer] => a
            [score] => 7
        )

    [b] => Array
        (
            [answer] => b
            [score] => 4
        )

    [h] => Array
        (
            [answer] => h
            [score] => 3
        )

)

Comments

0

I though of using a temporary array:

/* Current array */
$array = array(
    array("answer" => "a", "score" => 3),
    array("answer" => "b", "score" => 4),
    array("answer" => "h", "score" => 3),
    array("answer" => "a", "score" => 4)
);

/* Using a temporary array */
$tmp_array = array();
foreach($array as $subarray){
    if(array_key_exists($subarray["answer"], $tmp_array)){
        $tmp_array[$subarray["answer"]] += $subarray["score"];
    }else{
        $tmp_array[$subarray["answer"]] = $subarray["score"];
    }
}

/* Creating a new formatted array */
$new_array = array();
foreach($tmp_array as $key => $value){
    $new_array[] = array("answer" => $key, "score" => $value);
}

print_r($new_array);

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.