开发者

adding array values

开发者 https://www.devze.com 2022-12-23 11:52 出处:网络
Array ( [0] => Array ( [datas] => Array ( [name] => lorem [id] => 1 [type] => t1 [due_type] => Q1
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($to开发者_如何学JAVAtals[$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'];
    }
}


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

)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号