8

Okay, if feel like this should be really simple and accomplished by a function like array_merge() or array_merge_recursive, but I can't quite figure it out. I have two simple arrays structured like the (simplified) example below. I simply want to merge them into one array based on their index.

$array 1:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 

et cetera…

$array2 :

Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 

$desiredResult :

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
        [distance] => 1000 
        [time] => 10 
 )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
        [distance] => 1500 
        [time] => 15 
 )
) 

When I try to merge these using merge functions, I can only get this:

$unDesiredResult:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
  [2] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [3] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 

Do I need to loop through to push the second set into the first, or can this be done with an existing function?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Kerri
  • 1,211
  • 2
  • 15
  • 22

4 Answers4

14

I don't think there is a function to do this for you, you're gonna have to loop.

$result = array();
foreach($array1 as $key=>$val){ // Loop though one array
    $val2 = $array2[$key]; // Get the values from the other array
    $result[$key] = $val + $val2; // combine 'em
}

Or you can push the data into $array1, so you need to make a 3rd array:

foreach($array1 as $key=>&$val){ // Loop though one array
    $val2 = $array2[$key]; // Get the values from the other array
    $val += $val2; // combine 'em
}
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • So I do have to loop through (I always assume there's a quicker way that I'm missing). Thanks, this works perfectly (as may the below answer, but I'm going for the simplest). – Kerri Mar 02 '12 at 23:18
  • @Kerri: I couldn't find a built-in function that would do what you want. `array_merge_recursive` doesn't give the desired output. – gen_Eric Mar 02 '12 at 23:19
  • @Kerri: `array_merge_recursive` would work if your arrays had [string keys](http://ideone.com/xZQYd) (not [numeric keys](http://ideone.com/s8mHI)); – gen_Eric Mar 02 '12 at 23:26
  • 1
    @Kerri: The "hack" you can do is to add a prefix to the numbers, use `array_merge_recursive`, then strip the prefix, but that seems like more work. http://stackoverflow.com/q/2213156/206403 – gen_Eric Mar 02 '12 at 23:29
  • This is very useful answer. @Rocket Thanks a lot – Yagnesh bhalala Sep 19 '18 at 09:30
3

You'll need to use a loop. Try creating a function:

function addArray( array &$output, array $input ) {
    foreach( $input as $key => $value ) {
        if( is_array( $value ) ) {
            if( !isset( $output[$key] ) )
                $output[$key] = array( );
            addArray( $output[$key], $value );
        } else {
            $output[$key] = $value;
        }
    }
}

Then:

$combinedArray = array( );
addArray( $combinedArray, $array1 );
addArray( $combinedArray, $array2 );
kitti
  • 14,663
  • 31
  • 49
1

example Arrays has to be merged:

 [location_coordinates] => Array
                                (
                                    [0] => 36.037939100000,-78.905221600000
                                    [1] => 36.004398400000,-78.936084600000
                                )

                            [tm_field_reference_locations$field_location_address$city] => Array
                                (
                                    [0] => Durham
                                    [1] => Durham
                                )

                            [doctor_city] => Array
                                (
                                    [0] => Durham
                                    [1] => Durham
                                )

                            [tm_field_reference_locations$field_location_address$street] => Array
                                (
                                    [0] => 407 Crutchfield Street
                                    [1] => 40 Duke Medicine Circle
                                )

                            [tm_field_reference_locations$field_office_phone] => Array
                                (
                                    [0] => 919-479-4120
                                    [1] => 919-613-0444
                                )

                            [sm_field_reference_locations$title] => Array
                                (
                                    [0] => Duke Regional Hospital Spine and Neurosciences
                                    [1] => Duke Spine Center
                                )

When you loop like below:

        $address= array();
for($n=0; $n<sizeof($kolDetails['location_coordinates']); $n++){
    $address[$n]['org_institution_id']=$kolDetails['sm_field_reference_locations$title'][$n];
    $address[$n]['longitude']=$kolDetails['location_coordinates'][$n];
    $address[$n]['City']=$kolDetails['doctor_city'][$n];
    $address[$n]['address1']=$kolDetails['tm_field_reference_locations$field_location_address$street'][$n];                 
    $address[$n]['phone_number']=$kolDetails['tm_field_reference_locations$field_office_phone'][$n];        
     }
     $kolextra['adress']=$address;
     pr($kolextra['adress']);
     $address = array();
0

I'd go about this slightly differently with an interval. This assumes your values are always locked and that the first record of array1 always corresponds with the first record of array2:

$i = 0; // sets counter
$end = count($array1); // finds record number
while($i <= $end){ //for each record in the array
    $array1[$i]['distance'] = $array2[$i]['distance'];  //match up array values
    $array1[$i]['time'] = $array2[$i]['time'];  //match up array values
    $i++; //iterate
}

Good Luck!

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
AshBrad
  • 482
  • 2
  • 15
  • 1
    Thank you. This would work well too, and the assumptions would work in my use case. But there are actually a bunch more key/values that get set than in my example arrays, so this would end up being more cumbersome to code (and less flexible). But would be good if my arrays were actually as simple as in my example. – Kerri Mar 02 '12 at 23:23