Merge arrays

Hi guys,

I’ve got the following array:

Array
(
    [0] => Array
        (
            [id] => 17
            [interno] => 1A - Alessandro
            [Tabella generale] => 375.00
        )

    [1] => Array
        (
            [id] => 11
            [interno] => 1A - Giuseppe
            [Tabella generale] => 375.00
        )

    [2] => Array
        (
            [id] => 15
            [interno] => 1B - Francesco
            [Tabella generale] => 750.00
        )

    [3] => Array
        (
            [id] => 17
            [interno] => 1A - Alessandro
            [Tabella scale] => 125.00
        )

    [4] => Array
        (
            [id] => 11
            [interno] => 1A - Giuseppe
            [Tabella scale] => 125.00
        )

    [5] => Array
        (
            [id] => 15
            [interno] => 1B - Francesco
            [Tabella scale] => 250.00
        )

)

How can I merge the values with same “id” key? This is what I’m trying to achieve:

Array
(
    [0] => Array
        (
            [id] => 17
            [interno] => 1A - Alessandro
            [Tabella generale] => 375.00
            [Tabella scale] => 125.00
        )

    [1] => Array
        (
            [id] => 11
            [interno] => 1A - Giuseppe
            [Tabella generale] => 375.00
            [Tabella scale] => 125.00
        )

    [2] => Array
        (
            [id] => 15
            [interno] => 1B - Francesco
            [Tabella generale] => 750.00
            [Tabella scale] => 250.00
        )

)

many thanks for your help

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 -

$data[0] = ['id' => 17,
            'interno' => '1A - Alessandro',
            'Tabella generale' => 375.00,
        ];

$data[1] = ['id' => 11,
            'interno' => '1A - Giuseppe',
            'Tabella generale' => 375.00,
        ];

$data[2] = ['id' => 15,
            'interno' => '1B - Francesco',
            'Tabella generale' => 750.00,
        ];

$data[3] = ['id' => 17,
            'interno' => '1A - Alessandro',
            'Tabella scale' => 125.00,
        ];

$data[4] = ['id' => 11,
            'interno' => '1A - Giuseppe',
            'Tabella scale' => 125.00,
        ];

$data[5] = ['id' => 15,
            'interno' => '1B - Francesco',
            'Tabella scale' => 250.00,
        ];

$result = [];
foreach($data as $row)
{
	$result[$row['id']] = array_merge($result[$row['id']] ?? [],$row);
}

echo '<pre>'; print_r($result); echo '</pre>';

If the data in the array is coming from the database, you might want to look into the feasibility of getting the required data with a single query

1 Like

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

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.