简体   繁体   English

通过索引合并两个数组

[英]Merging two arrays by index

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. 好的,如果感觉这样应该很简单并且可以通过array_merge()array_merge_recursive类的函数来完成,但是我不太清楚。 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 1:

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

et cetera… 等等…

$array2 : $ array2:

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

$desiredResult : $ 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: $ 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? 我是否需要遍历才能将第二组推入第一组,或者可以使用现有功能来完成?

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: 或者,您可以将数据推送到$ array1中,因此需要制作第三个数组:

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

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 );

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();

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: 这假设您的值始终被锁定,并且array1的第一条记录始终与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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM