0
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'];
    }
}

2 Answers 2

1

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

)
Sign up to request clarification or add additional context in comments.

3 Comments

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

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.

3 Comments

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.
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'
I believe I couldn't use group by, see the output array above. So many occurences of type and due_type into one id.

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.