算法积累之处理数组数据
生活随笔
收集整理的這篇文章主要介紹了
算法积累之处理数组数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原數據
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
本文轉自TBHacker博客園博客,原文鏈接:http://www.cnblogs.com/jiqing9006/p/3796920.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的算法积累之处理数组数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win下svn常用操作笔记
- 下一篇: 最小栈的实现与优化