doujiang1913 2010-03-15 06:59
浏览 7
已采纳

添加数组值

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条回答 默认 最新

  • doutang1946 2010-03-15 08:02
    关注

    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
                   )
           )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)