2015-09-21 7 views
5

Ho una matrice come risultato di una query di database. Le linee includono due dimensioni e alcune metriche. Le metriche devono essere sommate dai gruppi di dimensioni.PHP Raggruppamento di una matrice con più dimensioni dai risultati del database

Ecco un esempio grezzo array di dati in vista tabella: enter image description here

Ecco dell'array esatta:

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) 
    } 
} 

Ecco quello che mi aspetto di ottenere in uscita: enter image description here

Qual è il modo migliore per ottenere quell'output?

Grazie.

+0

Quindi, loop sull'array e somma i valori in una nuova matrice. Non mi è chiaro dove sei bloccato. – GolezTrol

+0

Questa matrice è il risultato di una query del database? – CodeGodie

+0

@CodeGodie in realtà non riuscivo a capire come posso raggruppare e sommare senza loop multipli. E, sì, è il risultato di una query del database. – user1488895

risposta

5

Si può semplicemente fare foreach qui come come

$result = []; 
foreach($data as $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)); 
+1

Non capisco cosa sta succedendo in quel altro ramo ... Non dovrebbe essere '$ result [$ hash] = $ valore;'? – GolezTrol

+0

OMG! Non ho visto che ho aggiornato la mia risposta @GolezTrol.Grazie per aver segnalato che ho appena postato la risposta e chiuso la finestra –

+0

@ GolezTrol Anche io sto pensando perché non è stato downvoted –

3

Lei ha citato questa matrice è il risultato di una query di database. Quindi, non dovresti iterare attraverso i risultati come questo, il tuo focus dovrebbe essere nel modo in cui stai ottenendo questi risultati dal tuo database in quanto SQL può fare tutto questo per te con prestazioni migliori.

di mostrarvi questo, immaginare la vostra tabella di database è denominato my_table e dispone di tutte le informazioni che avete inviato sopra: (source_name, week, picks, won, lost, draw):

+-------------+------+-------+-----+------+------+ 
| source_name | week | picks | won | lost | draw | 
+-------------+------+-------+-----+------+------+ 
| A   | 10 | 1  | 0 | 1 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 10 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 11 | 1  | 0 | 1 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 11 | 1  | 0 | 1 | 0 | 
+-------------+------+-------+-----+------+------+ 
| A   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| B   | 10 | 1  | 0 | 1 | 0 | 
+-------------+------+-------+-----+------+------+ 
| B   | 10 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| B   | 11 | 1  | 0 | 1 | 0 | 
+-------------+------+-------+-----+------+------+ 
| B   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| C   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 
| C   | 11 | 1  | 1 | 0 | 0 | 
+-------------+------+-------+-----+------+------+ 

Se si esegue il seguendo la query SQL, otterrete i risultati desiderati senza dovervi preoccupare di iterare o eseguire il ciclo successivo.

SELECT source_name, week, sum(picks), sum(won), sum(lost), sum(draw) 
FROM my_table 
GROUP BY source_name, week 
ORDER BY source_name 

RISULTATO:

+-------------+------+------------+----------+-----------+-----------+ 
| source_name | week | sum(picks) | sum(won) | sum(lost) | sum(draw) | 
+-------------+------+------------+----------+-----------+-----------+ 
| A   | 10 | 2   | 1  | 1   | 0   | 
+-------------+------+------------+----------+-----------+-----------+ 
| A   | 11 | 5   | 3  | 2   | 0   | 
+-------------+------+------------+----------+-----------+-----------+ 
| B   | 10 | 2   | 1  | 1   | 0   | 
+-------------+------+------------+----------+-----------+-----------+ 
| B   | 11 | 2   | 1  | 1   | 0   | 
+-------------+------+------------+----------+-----------+-----------+ 
| C   | 11 | 2   | 2  | 0   | 0   | 
+-------------+------+------------+----------+-----------+-----------+ 

Dai un'occhiata a questo SQL FIDDLE per aiutarvi a capire che.