Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Merge arrays in PHP based on keys

Posted on 2013-11-01
4
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Julian Hansen earned 2000 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 59

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 111

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

715 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