4
回答
php数组重组?还是只要用某个算法输出即可?
注册华为云得mate10,2.9折抢先购!>>>   

有个数据库读出来的数据集是这样的:

$array = array(
	0 = array ('cid' => 101, 'score' => 55),
	1 = array ('cid' => 101, 'score' => 35),
	2 = array ('cid' => 101, 'score' => -15),
	3 = array ('cid' => 103, 'score' => 35),
	4 = array ('cid' => 106, 'score' => 75),
	5 = array ('cid' => 106, 'score' => -35),
	6 = array ('cid' => 108, 'score' => 35),
)

现在输出结果想这样(cid固定从101变化到110),即对每个cid都要进行汇总,没有分数的也要输出0:

cid   score              
----------------------
101   55
101   35
101   -15
101   75     101汇总
102   0
102   0      102汇总
103   35
103   35     103汇总
104   0
104   0      104汇总
105   0
105   0      105汇总
106   75
106   -35
106   40     106汇总
......

怎么输出?

PHP
举报
oreax
发帖于4年前 4回/268阅
共有4个答案 最后回答: 4年前
$arr = array(
	 array ('cid' => 101, 'score' => 55),
	 array ('cid' => 101, 'score' => 35),
	 array ('cid' => 101, 'score' => -15),
	 array ('cid' => 103, 'score' => 35),
	 array ('cid' => 106, 'score' => 75),
	 array ('cid' => 106, 'score' => -35),
	 array ('cid' => 108, 'score' => 35),
);

$new1 = array();
foreach ($arr as $key => $value) {
    $new1[$value['cid']][] = $value['score'];
}
$new2 = array();
foreach($new1 as $k=>$v){
    $new2[] = array('cid'=>$k,'score'=>$v, 'scoreTotal'=>array_sum($v));
}

print_r($new2);

$cidArr = array_flip(range(101, 110));
foreach($new2 as $item){
    $cidArr[$item['cid']] = $item;
}
foreach($cidArr as $cid => $val){
  if(is_array($val)){
    $cidArr[$cid] = array('cid'=>0, 'score'=>0, 'scoreTotal'=>0);
  }
}

print_r(array_values($cidArr));



  
// result is:
Array
(
    [0] => Array
        (
            [cid] => 101
            [score] => Array
                (
                    [0] => 55
                    [1] => 35
                    [2] => -15
                )

            [scoreTotal] => 75
        )

    [1] => Array
        (
            [cid] => 103
            [score] => Array
                (
                    [0] => 35
                )

            [scoreTotal] => 35
        )

    [2] => Array
        (
            [cid] => 106
            [score] => Array
                (
                    [0] => 75
                    [1] => -35
                )

            [scoreTotal] => 40
        )

    [3] => Array
        (
            [cid] => 108
            [score] => Array
                (
                    [0] => 35
                )

            [scoreTotal] => 35
        )

)
Array
(
    [0] => Array
        (
            [cid] => 0
            [score] => 0
            [scoreTotal] => 0
        )

    [1] => 1
    [2] => Array
        (
            [cid] => 0
            [score] => 0
            [scoreTotal] => 0
        )

    [3] => 3
    [4] => 4
    [5] => Array
        (
            [cid] => 0
            [score] => 0
            [scoreTotal] => 0
        )

    [6] => 6
    [7] => Array
        (
            [cid] => 0
            [score] => 0
            [scoreTotal] => 0
        )

    [8] => 8
    [9] => 9
)



排列好的话

遍历数组,把 cid值赋给另外一个变量

循环中加入判断,看cid值是不是和变量值相等

相等的话,求和

不相等的话,把求和的变量值清0

顶部