php - Find interception of two arrays and then merge the two -
i have large array of hostnames , 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 comparing smaller array - same host name links 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 create 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 find intersection of arrays , merge 2 (find same key , merge). i've tried various functions end result never want.
i managed find function here merge common keys, returned ip in array in array containing location fields in array, 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 easier way this?
pretty easy built-in php function:
$result = array_merge_recursive($large, $small);
based on comment values common , merge. assumes keys in $small
in $large
:
$result = array_merge_recursive(array_intersect_key($large, $small), $small);
to not make assumption, if there may keys in either 1 aren't in other then:
$result = array_merge_recursive(array_intersect_key($large, $small), array_intersect_key($small, $large));
Comments
Post a Comment