2012-03-02 1 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 
) 
) 

, I는이를 얻을 수 있습니다 :

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

아니면 제 3의 배열을 만들 필요가 그래서 당신은 $ 배열 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

+0

@Kerri : 원하는 기능을 수행 할 수있는 내장 함수를 찾을 수 없습니다. 'array_merge_recursive'는 원하는 출력을 제공하지 않습니다. –

+0

@Kerri : 배열에 [string keys] (http://ideone.com/xZQYd)가 있으면 ([숫자 키]가 아닌 경우) http : // ideone.com/s8mHI)); –

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합니다. –

+0

'addArray ($ array1, $ array2);'를 사용하는 것이 더 쉽지 않을까요? –

0

내가 간격으로 약간 다르게 이것에 대해 갈 것. 이것은 값이 항상 잠겨 있고 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 
} 

행운을 비네!

+1

감사합니다. 이것은 잘 작동 할 것이고, 가정은 나의 유스 케이스에서 작동 할 것이다. 그러나 실제로 예제 배열보다 더 많은 키/값이 설정되므로 코드 작성이 더 복잡해지고 유연성이 떨어진다는 단점이 있습니다. 내 배열이 내 예제처럼 간단하다면 좋을 것입니다. – Kerri

+0

의견에 감사드립니다. – AshBrad

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