Solved

Merge arrays in PHP based on keys

Posted on 2013-11-01
4
350 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 55

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 55

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 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

861 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