If this data is being fetched from a query or some other data source, you should index/pivot it using the id value as the main array index. You would directly get the result you are after. If not, then see the following -
Hi @mabismad many thanks for your help it does work. Yes the data is fetched from a query, I’m still learning how to pivot in mysql and php (I find it quite difficult). This is my query:
SELECT tabelle_millesimali.descrizione as tabella,
millesimi.valore as millesimi,
immobili.interno,
anagrafica.user_first_name,
anagrafica.id,
proprietari.quota,
TRUNCATE( ((SUM(sottoconti_spese.importo)*millesimi.valore)/1000)*proprietari.quota/100 ,2) as quota
FROM sottoconti_spese
LEFT JOIN sottoconto_tabella
ON sottoconti_spese.id = sottoconto_tabella.sottoconto
LEFT JOIN tabelle_millesimali
ON tabelle_millesimali.id = sottoconto_tabella.tabella_millesimale
LEFT JOIN millesimi
ON millesimi.tabella_millesimale = tabelle_millesimali.id
LEFT JOIN immobili
ON immobili.id = millesimi.immobile
LEFT JOIN proprietari
ON proprietari.immobile_id = immobili.id
LEFT JOIN anagrafica
ON anagrafica.id = proprietari.anagrafica_id
WHERE sottoconti_spese.preventivo_id = 1
AND sottoconti_spese.building_id = 17
GROUP BY sottoconti_spese.conto_spesa,
sottoconto_tabella.coefficiente,
millesimi.valore,
immobili.interno,
anagrafica.user_first_name,
proprietari.quota,
tabelle_millesimali.descrizione,
anagrafica.id
I don’t really know where to start how to pivot this query from mysql. I would be very gratefull if you could please give me an example on how to change this query. I understand that is better to pivot with mysql but I also I would like to ask if there is any php library to make life easier when it comes to create a pivot table. Many thanks