mysql - PHP Grouping an Array with Multiple Dimensions from database results -
i have array result of database query. lines include 2 dimensions , metrics. metrics must summed dimension groups.
here example raw data array in table view:
here exact array:
array(13) { [0]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "10" ["picks"]=> int(1) ["won"]=> int(0) ["lost"]=> int(1) ["draw"]=> int(0) } [1]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "10" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [2]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [3]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [4]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(0) ["lost"]=> int(1) ["draw"]=> int(0) } [5]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(0) ["lost"]=> int(1) ["draw"]=> int(0) } [6]=> array(6) { ["source_name"]=> string(8) "a" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [7]=> array(6) { ["source_name"]=> string(7) "b" ["week"]=> string(2) "10" ["picks"]=> int(1) ["won"]=> int(0) ["lost"]=> int(1) ["draw"]=> int(0) } [8]=> array(6) { ["source_name"]=> string(7) "b" ["week"]=> string(2) "10" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [9]=> array(6) { ["source_name"]=> string(7) "b" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(0) ["lost"]=> int(1) ["draw"]=> int(0) } [10]=> array(6) { ["source_name"]=> string(7) "b" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [11]=> array(6) { ["source_name"]=> string(9) "c" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } [12]=> array(6) { ["source_name"]=> string(9) "c" ["week"]=> string(2) "11" ["picks"]=> int(1) ["won"]=> int(1) ["lost"]=> int(0) ["draw"]=> int(0) } }
what best way output?
thanks.
you can foreach
on here as
$result = []; foreach($data $key => $value){ $hash = $value['source_name'] ."_". $value['week']; if(isset($result[$hash])){ $result[$hash]['picks'] += $value['picks']; $result[$hash]['won'] += $value['won']; $result[$hash]['lost'] += $value['lost']; $result[$hash]['draw'] += $value['draw']; }else{ $result[$hash] = $value; } } print_r(array_values($result));
Comments
Post a Comment