Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按索引合并两个数组_Php_Arrays_Array Merge - Fatal编程技术网

Php 按索引合并两个数组

Php 按索引合并两个数组,php,arrays,array-merge,Php,Arrays,Array Merge,好的,如果我觉得这应该非常简单,并且通过一个函数来完成,比如array\u merge()或者array\u merge\u recursive,但是我不太明白。我有两个结构简单的数组,如下面的(简化)示例所示。我只是想根据它们的索引将它们合并到一个数组中 $array 1: Array ( [0] => Array ( [ID] => 201 [latLng] => 45.5234515, -122.6762071 )

好的,如果我觉得这应该非常简单,并且通过一个函数来完成,比如
array\u merge()
或者
array\u merge\u recursive
,但是我不太明白。我有两个结构简单的数组,如下面的(简化)示例所示。我只是想根据它们的索引将它们合并到一个数组中

$array 1:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
等等

$array2:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
$desiredResult:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
当我尝试使用合并函数合并这些时,我只能得到以下结果:

$unDesiredResult:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();

我是否需要通过循环将第二个集合推入第一个集合,或者这可以通过现有的函数来完成?

我认为没有函数可以为您执行此操作,您必须循环

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

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

我不认为有一个函数可以为你做这件事,你必须循环

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

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

你需要使用一个循环。尝试创建一个函数:

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;
        }
    }
}
然后:


你需要使用一个循环。尝试创建一个函数:

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;
        }
    }
}
然后:


我会在一段时间内稍微改变一下。这假设您的值始终处于锁定状态,并且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
}
$i=0;//设置计数器
$end=计数($array1);//查找记录编号

而($i我会以稍微不同的间隔进行处理。这假设您的值始终被锁定,并且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
}
$i=0;//设置计数器
$end=count($array1);//查找记录编号

而($i示例数组必须合并:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
当您像下面这样循环时:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
$address=array();

对于($n=0;$n示例必须合并数组:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
当您像下面这样循环时:

Array ( 
  [0] => Array ( 
        [ID] => 201 
        [latLng] => 45.5234515, -122.6762071 
  )  
  [1] => Array ( 
        [ID] => 199 
        [latLng] => 37.7931446, -122.39466520000002 
  )
) 
Array ( 
  [0] => Array ( 
        [distance] => 1000 
        [time] => 10 
  )  
  [1] => Array ( 
        [distance] => 1500 
        [time] => 15 
  )
) 
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 
 )
) 
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 
  )
) 
 [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();
$address=array();

对于($n=0;$n只做
addArray($array1,$array2);
?只做
addArray($array1,$array2);
难道不更容易吗?所以我必须循环(我总是认为我错过了一种更快的方法)。谢谢,这非常有效(可能是下面的答案,但我选择最简单的).@Kerri:我找不到一个可以做你想做的事情的内置函数。
array\u merge\u recursive
无法提供所需的输出。@Kerri:
array\u merge\u recursive
如果你的数组有(没有);@Kerri:hack您可以做的是在数字中添加前缀,使用
数组\u合并\u递归
,然后去掉前缀,但这似乎需要更多的工作。这是一个非常有用的答案。@Rocket谢谢,所以我必须循环(我总是认为有一种更快的方法我错过了)。谢谢,这非常有效(可能是下面的答案,但我选择的是最简单的)。@Kerri:我找不到一个可以满足您需要的内置函数。
array\u merge\u recursive
无法提供所需的输出。@Kerri:
array\u merge\u recursive
如果您的数组有(没有);@Kerri:hack您可以做的是在数字中添加前缀,使用
数组\u合并\u递归
,然后去掉前缀,但这似乎需要更多的工作。这是一个非常有用的答案。@Rocket非常感谢谢谢您。这也会很好地工作,并且假设在我的用例中也会起作用。但实际上,设置的键/值比我的ex中的要多足够多的数组,因此这将导致编写代码更加繁琐(并且灵活性较差)。但如果我的数组实际上像我的示例中那样简单,那就太好了。谢谢。这也会很好地工作,并且假设在我的用例中也会工作。但是实际上,与我的示例数组相比,设置的键/值要多很多,因此这最终会导致编写代码更麻烦(而且灵活性更低)。但如果我的数组实际上像我的示例中那样简单,那就太好了。