算法积累之处理数组数据

原数据

Array
(
    [0] => Array
        (
            [award_cat] => 1
            [award_level] => 1
            [count] => 5
        )

    [1] => Array
        (
            [award_cat] => 2
            [award_level] => 1
            [count] => 11
        )

    [2] => Array
        (
            [award_cat] => 2
            [award_level] => 2
            [count] => 6
        )

    [3] => Array
        (
            [award_cat] => 2
            [award_level] => 3
            [count] => 7
        )

    [4] => Array
        (
            [award_cat] => 2
            [award_level] => 4
            [count] => 10
        )

    [5] => Array
        (
            [award_cat] => 2
            [award_level] => 5
            [count] => 10
        )

    [6] => Array
        (
            [award_cat] => 2
            [award_level] => 6
            [count] => 9
        )

    [7] => Array
        (
            [award_cat] => 2
            [award_level] => 7
            [count] => 7
        )

    [8] => Array
        (
            [award_cat] => 3
            [award_level] => 2
            [count] => 5
        )

    [9] => Array
        (
            [award_cat] => 4
            [award_level] => 3
            [count] => 5
        )

    [10] => Array
        (
            [award_cat] => 5
            [award_level] => 4
            [count] => 4
        )

    [11] => Array
        (
            [award_cat] => 6
            [award_level] => 6
            [count] => 6
        )

)

目标数据

$outData = array(0=>array('name'=>'论文','data'=>'1,2,3,4,5,6,7'),1=>array('name'=>'优质课','data'=>'3,4,6,9,4,8,10'));

 

思路:

先对原数据进行一次简单处理,

foreach ($aAwardMem as $k => $v) {
            $a[$v['award_cat']][$v['award_level']]=$v;
        }

处理之后为,

[1] => Array
        (
            [1] => Array
                (
                    [award_cat] => 1
                    [award_level] => 1
                    [count] => 5
                )

        )

    [2] => Array
        (
            [1] => Array
                (
                    [award_cat] => 2
                    [award_level] => 1
                    [count] => 11
                )

            [2] => Array
                (
                    [award_cat] => 2
                    [award_level] => 2
                    [count] => 6
                )

            [3] => Array
                (
                    [award_cat] => 2
                    [award_level] => 3
                    [count] => 7
                )

            [4] => Array
                (
                    [award_cat] => 2
                    [award_level] => 4
                    [count] => 10
                )

            [5] => Array
                (
                    [award_cat] => 2
                    [award_level] => 5
                    [count] => 10
                )

            [6] => Array
                (
                    [award_cat] => 2
                    [award_level] => 6
                    [count] => 9
                )

            [7] => Array
                (
                    [award_cat] => 2
                    [award_level] => 7
                    [count] => 7
                )

        )

    [3] => Array
        (
            [2] => Array
                (
                    [award_cat] => 3
                    [award_level] => 2
                    [count] => 5
                )

        )

    [4] => Array
        (
            [3] => Array
                (
                    [award_cat] => 4
                    [award_level] => 3
                    [count] => 5
                )

        )

    [5] => Array
        (
            [4] => Array
                (
                    [award_cat] => 5
                    [award_level] => 4
                    [count] => 4
                )

        )

    [6] => Array
        (
            [6] => Array
                (
                    [award_cat] => 6
                    [award_level] => 6
                    [count] => 6
                )

        )

这样key值就有意义了。

第一个key表示award_cat

第二个表示award_level

 

然后再对数据,进行字符串处理。

foreach ($researchAwardCat as $i => $ival) {//论文
            $outData[$i-1]['name'] = $ival;
            $outstr = '';
            $count = 0;
            foreach ($researchAwardLevel as $j => $jval) {//国际、国家
                if($count!=0){
                    $outstr.=',';
                }
                if(isset($a[$i][$j])){
                        $outstr .=$a[$i][$j]['count'];
                }else{
                        $outstr .='0';
                }
                $count++;
            }
            $outData[$i-1]['data'] = $outstr;
        }

这样就达到最终数据模型了。

Array
(
    [0] => Array
        (
            [name] => 论文
            [data] => 5,0,0,0,0,0,0
        )

    [1] => Array
        (
            [name] => 优质课
            [data] => 11,6,7,10,10,9,7
        )

    [2] => Array
        (
            [name] => 课件
            [data] => 0,5,0,0,0,0,0
        )

    [3] => Array
        (
            [name] => 教学设计
            [data] => 0,0,5,0,0,0,0
        )

    [4] => Array
        (
            [name] => 基本功赛
            [data] => 0,0,0,4,0,0,0
        )

    [5] => Array
        (
            [name] => 带生获奖
            [data] => 0,0,0,0,0,6,0
        )

)

就能看到统计图了,yeah

posted @ 2014-06-19 16:20  TBHacker  阅读(319)  评论(0编辑  收藏  举报