Solved

Merge arrays in PHP based on keys

Posted on 2013-11-01
4
348 Views
Last Modified: 2013-11-05
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.
0
Comment
Question by:ltpitt
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39617871
This should do it - assume the first array is called $firstarray and the second $secondarray
// First build an indexed array on 
// id_decoder (as the key) and 
// the first array index as the value

$keys = array();
foreach($firstarray as $k=>$f)
{
    $keys[$f['id_decoder']] = $k;
}

// Now it is trival to loop through the second array
// use the keys array to find the correct index in
// the first array to add the mesi_pagati field

foreach($secondarray as $s) {
    $firstarray[$keys[$s['id_decoder']]]['mesi_pagati'] = $s['mesi_pagati'];
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:ltpitt
ID: 39617941
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.
0
 
LVL 52

Expert Comment

by:Julian Hansen
ID: 39618640
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39624735
Some good learning resources are identified in this article.  
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The Welling-Thompson book might be perfect!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 81
Create new password with id in link 5 25
How to loop through records  using SELET COUNT 2 13
Why my select dropdown does not work? 8 21
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now