tags:

views:

66

answers:

2
Array
(
[0] => Array
    (
        [datas] => Array
            (
                [name] => lorem
                [id] => 1
                [type] => t1
                [due_type] => Q1
                [t1] => 1
                [t2] => 1
                [t3] => 1
            )

    )

[1] => Array
    (
        [datas] => Array
            (
                [name] => lorem
                [id] => 1
                [type] => t2
                [due_type] => Q1
                [t1] => 0
                [t2] => 1
                [t3] => 0
            )

    )

[2] => Array
    (
        [datas] => Array
            (
                [name] => name
                [id] => 2
                [type] => t1
                [due_type] => Q1
                [t1] => 1
                [t2] => 0
                [t3] => 1
            )

    )

[3] => Array
    (
        [datas] => Array
            (
                [name] => name
                [id] => 2
                [type] => t2
                [due_type] => Q1
                [t1] => 1
                [t2] => 0
                [t3] => 0
            )

    )

)

I want to add the values of each array according to its id, but I am having problem getting the values using these code: I want to compute the sum of all type according to each due_type and combining them into one array.

$totals = array();
$i = -1;
foreach($datas as $key => $row){
    $i += 1;
    $items[$i] = $row;

    if (isset($totals[$items[$i]['datas']['id']])){
        if($totals[$items[$i]['datas']['id']]['due_type'] == 'Q1'){

            if($totals[$items[$i]['datas']['id']]['type'] == 't1'){
                $t1+=$totals[$items[$i]['datas']['id']]['t1'];
            }elseif($totals[$items[$i]['datas']['id']]['type'] == 't2'){
                $t2+=$totals[$items[$i]['datas']['id']]['t2'];
            }elseif($totals[$items[$i]['datas']['id']]['type'] == 't3'){
                $t3+=$totals[$items[$i]['datas']['id']]['t3'];
            }
            $totals[$items[$i]['datas']['id']]['t1_total'] = $t1;
                    $totals[$items[$i]['datas']['id']]['t2_total'] = $t2;
        }
    }
    else {
        $totals[$items[$i]['datas']['id']] = $row['datas'];
        $totals[$items[$i]['datas']['id']]['t1_total'] = $items[$i]['datas']['t1'];
            $totals[$items[$i]['datas']['id']]['t2_total'] = $items[$i]['datas']['t2'];
    }
}
A: 

I might be wrong, but it feels like you are getting those arrays from a database read. It might be easier to do such an operation in the database itself:

A SQL statement like this would do what you intend to do:

SELECT count(1),type,due_type FROM Table GROUP BY type, due_type;

You can read the output back in PHP.

aip.cd.aish
Yes you are right, those array are from database read. but I cannot use a group by statement in my sql query because of due field in my table. due_date field is needed to check if the item is overdue or not.
christian
Couldn't you just add a HAVING clause to your SQL statement. Something along the line of the `SELECT ... FROM Table GROUP BY type, due_type HAVING due = 'overdue'`
aip.cd.aish
I believe I couldn't use group by, see the output array above. So many occurences of type and due_type into one id.
christian
+1  A: 

Not sure what output you want, but this:

function condense($data, $condensed = array()) {

    foreach($data as $row) {
        $id = $row['datas']['id'];
        $due_type = $row['datas']['due_type'];
        $type = $row['datas']['type'];
        if(!array_key_exists($id, $condensed)) {
            $condensed[$id] = array();
        }
        if(!array_key_exists($due_type, $condensed[$id])) {
            $condensed[$id][$due_type] = array();
        }
        if(!array_key_exists($type, $condensed[$id][$due_type])) {
            $condensed[$id][$due_type][$type] = 0;
        }
        $condensed[$id][$due_type][$type] += $row['datas'][$type];
    }

    return $condensed;
}

$result = condense($data);

gives you:

Array
(
    [1] => Array
       (
           [Q1] => Array
               (
                  [t1] => 1
                  [t2] => 1
               )
       )
    [2] => Array
       (
           [Q1] => Array
               (
                  [t1] => 1
                  [t2] => 0
               )
       )

)
Felix Kling
Try to add another array item ex t1, it doesn't add to the previous t1.
christian
Thanks! you don't need to include the last condition in the code.
christian
@christian: I corrected my code, there was a minor mistake. And I just wanted to have all values initialized properly. Do whatever fits you ;)
Felix Kling