2012-03-02 98 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();