Solved

Merge arrays in PHP based on keys

Posted on 2013-11-01
4
347 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 51

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 51

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

760 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

18 Experts available now in Live!

Get 1:1 Help Now