0

I have this array :

 $data =    Array
    (     
        [1] => Array
            (
                [id] => '52040344'
                [outcome] => 0
                [names] => Array
                    (
                      [name_eng] => 'this is a name 2' 
                    )    
            )

        [2] => Array
            (
                [id] => 54030157
                [outcome] => 1108541
                [names] => Array
                    (
                         [name_eng] => 'this is a name 1' 
                    )    
            )

        [3] => Array
            (
                [id] => '54030157
                [outcome] => '109635'
                [names] => Array
                    (
                        [name_eng] => 'this is a name 1' 
                    )
            )
    )

i want to to return an array with the sum of all outcome having the same id, knowing that if they have same id, they have same name.

so i can get

array (
    [0] => array( 'id' => '54030157', 'outcome' => 'sum of outcome', 'name' => 'this is name 1' ),
    [1] => array(  'id' => '52040344', 'outcome' => 'sum of outcome', 'name' => 'this is name 2' )
)

how can i get this done ??.

here is what i have tried so far :

 public function process($data){                        
        $outputArray = array();

        foreach ($data as $key => $value) {                                 

        }            
         return $outputArray;
    }

any help would be appreciated

2 Answers 2

1

Edit : Answered before update of original answer! Not taken notice of some special constructed output array, but only the calculation of outcome.

Here is a little function you can use :

function computeOutcome($array) {
    $result=array();
    foreach ($array as $item) {
        $id=$item['id'];
        if (isset($result[$id])) {
            $result[$id]=$result[$id]+$item['outcome'];
        } else {
            $result[$id]=$item['outcome'];
        }
    }
    return $result;
}

Pass your multidimensional array as parameter. Test example :

$test=array(
    array('id'=>100, 'outcome'=>'45'),
    array('id'=>200, 'outcome'=>'66'),
    array('id'=>100, 'outcome'=>'88'),
    array('id'=>200, 'outcome'=>'45')
);
print_r(computeOutcome($test));

outputs

Array
(
    [100] => 133
    [200] => 111
)
Sign up to request clarification or add additional context in comments.

Comments

0

Or you can use array_walk:

$returnedArray = array();

function makeArray(&$element)
{
    global $returnedArray;

    $returnedArray[$element['id']]['id'] = $element['id'];
    @$returnedArray[$element['id']]['outcome'] += $element['outcome'];
    $returnedArray[$element['id']]['name'] = $element['names']['name_eng'];

}

array_walk($data, 'makeArray');

var_dump($returnedArray);

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.