0

I have a large array of hostnames and their corresponding location.

 Array ( [ABC01] => Array ( [hostid] => 12345 
                            [lat] => 123
                            [lon] => 123
                            [adr] => 126 Foo Street
                            [city] => Rocky Hill 
                            [state] => Connecticut
                            [country] => USA ) 
         [ABC02] => Array ( [hostid] => 12346 
                            [lat] => 345
                            [lon] => 345
                            [adr] => 123 Foo Street 
                            [city] => Boston 
                            [state] => Massachusetts
                            [country] => USA )

         [ABC03] => Array ( [hostid] => 12346 
                            [lat] => 345
                            [lon] => 345
                            [adr] => 123 Foo Street 
                            [city] => New York City
                            [state] => New York
                            [country] => USA )
          .....)

I am comparing it to a much smaller array - same host name but it links to the IP address:

 Array ( [ABC01] => Array ( [ip] => 192.168.2.1 ) 
         [ABC02] => Array ( [ip] => 192.168.2.2 )
         [ABC03] => Array ( [ip] => 192.168.2.3 )
       )

I want to create the following array:

         [ABC02] => Array ( [hostid] => 12346 
                            [lat] => 345
                            [lon] => 345
                            [adr] => 123 Foo Street 
                            [city] => Boston 
                            [state] => Massachusetts
                            [country] => USA
                            [ip] => 192.168.2.1)

I'm trying to find the intersection of the arrays and then merge the two (find the same key and then merge). I've tried various functions but the end result is never what I want.

I managed to find a function here to merge the common keys, but it returned the IP in an array in the array containing the location fields in another array, as seen below:

Array ( [ABC02] => Array ( [0] => Array ( [hostid] => 12346 
                           [lat] => 345
                           [lon] => 345
                           [adr] => 123 Foo Street 
                           [city] => Boston 
                           [state] => Massachusetts
                           [country] => USA
                           [1] => Array ( [ip] => 192.168.2.1) ) )

Is there an easier way to do this?

Community
  • 1
  • 1
l33tspeak
  • 95
  • 3
  • 13

1 Answers1

4

Pretty easy with a built-in PHP function:

$result = array_merge_recursive($large, $small);

Based on your comment to get only values that are common and then merge. This assumes all keys in $small are in $large:

$result = array_merge_recursive(array_intersect_key($large, $small), $small);

To not make the assumption, if there may be keys in either one that aren't in the other then:

$result = array_merge_recursive(array_intersect_key($large, $small),
                                array_intersect_key($small, $large));
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Sorry I did not specify: I don't want a full-on merge of the two - I want the interception of the two and then to merge the corresponding values from that larger array into the smaller. With array_merge_recursive, I get the full list. I know I can do an array_intercept_key again with the smaller array to give me what I want - it just seemed repetitive. – l33tspeak Apr 01 '15 at 20:31
  • Edited. If I now understand, then it's still pretty simple. – AbraCadaver Apr 01 '15 at 20:34
  • thanks - that did the trick! I started to do a front end project so I only returned to this seemingly tedious task now. – l33tspeak Apr 07 '15 at 14:06
  • Actually.. I just printed out the array and it's the hosts with only their IPs, not with the location fields :( – l33tspeak Apr 07 '15 at 14:17