Link to home
Start Free TrialLog in
Avatar of ltpitt
ltpitt

asked on

Merge arrays in PHP based on keys

I have this array:

Array
(
    [0] => Array
        (
            [id_decoder] => 1
            [id_cliente] => 1
            [id_gruppo] => 2
            [decoder] => decoder1
            [username] => decoder1
            [password] => decoder1
            [note] => decoder1
            [nome] => primo
            [cognome] => primini
            [nome_gruppo] => mascia
        )

    [1] => Array
        (
            [id_decoder] => 2
            [id_cliente] => 2
            [id_gruppo] => 1
            [decoder] => decoder2
            [username] => decoder2
            [password] => decoder2
            [note] => decoder2
            [nome] => secondo
            [cognome] => secondini
            [nome_gruppo] => miei
        )

    [2] => Array
        (
            [id_decoder] => 3
            [id_cliente] => 3
            [id_gruppo] => 2
            [decoder] => decoder3
            [username] => decoder3
            [password] => decoder3
            [note] => decoder3
            [nome] => Terzo
            [cognome] => Terzini
            [nome_gruppo] => mascia
        )

)

Open in new window


And this array:


Array
(
    [id_decoder] => 1
    [mesi_pagati] => 2
)
Array
(
    [id_decoder] => 2
    [mesi_pagati] => 1
)

Open in new window


I would like to merge the "mesi_pagati" only to its relative id_decoder.

How can I obtain that in php?

Maybe part of the problem is that the second array is made by two arrays.

I obtain this with this code:

$row = $result->fetch_array(MYSQLI_ASSOC);

Open in new window


But I can change it to numeric or to anything else if helps obtain the final result of a single array with all needed data.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ltpitt
ltpitt

ASKER

It works as a dream.

Can I ask you something really important for me?

Where can I learn to handle arrays like that?

I am doing code school and code academy but exercises are basic on arrays and on for loops.

I am missing something that allows me to be that great.

Thanks a ton for the perfect solution.
The key is to practice and look at other code.

There is only so much you can get out of a book - if you have the basics the rest is about application.

Thanks for the points - and you are most welcome