2012-03-02 99 views
5

好吧,合并两个数组,如果觉得这应该是非常简单和类似array_merge()array_merge_recursive一个函数来完成,但我不能完全弄清楚。我有两个简单的数组结构像下面的(简化)例子。我只是想根据它们的索引将它们合并到一个数组中。通过索引

$阵列1:

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

等等...

$数组2:

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

当我尝试这些使用合并功能合并,我只能得到这样的:

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

我需要遍历第二组推入第一,或者这可以用现有的功能完成?

回答

4

我不认为有一个函数来为你做这个,你会不得不循环。

$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 
} 

或者你也可以将数据推入$阵列1,所以你需要一个第三阵列:

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

所以我通过(我总是假设有一个更快的方法有循环,我丢了)。谢谢,这完美地工作(可能下面的答案,但我要做的最简单)。 – Kerri 2012-03-02 23:18:10

+0

@Kerri:我找不到可以做你想做的内置函数。 'array_merge_recursive'不能提供所需的输出。 – 2012-03-02 23:19:05

+0

@Kerri:'array_merge_recursive'在你的数组有[string keys](http://ideone.com/xZQYd)(不是[数字键](http:// ideone。COM/s8mHI)); – 2012-03-02 23:26:34

3

你需要使用一个循环。尝试创建一个功能:

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

然后:

$combinedArray = array(); 
addArray($combinedArray, $array1); 
addArray($combinedArray, $array2); 
+0

+1对+1 +1对于递归。 – 2012-03-02 23:16:41

+0

只需要执行'addArray($ array1,$ array2);'? – 2012-03-02 23:17:27

0

我会去了解这个略有不同的一个区间。这是假设你的价值观始终锁定,阵列1的第一个记录总是与数组2的第一条记录对应:

$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 
} 

祝您好运!

+1

谢谢。这也可以工作,假设可以在我的用例中起作用。但实际上有更多的键/值比我的示例数组中的更多,所以这最终会导致代码更笨重(并且不太灵活)。但是如果我的数组实际上和我的例子一样简单,那将会很好。 – Kerri 2012-03-02 23:23:12

+0

感谢您的反馈! – AshBrad 2012-03-03 00:42:46

2

例如阵列已被合并:

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

当你循环象下面这样:

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