合并这样的两个数组

时间:2017-03-02 06:41:52

标签: arrays array-merge

我有两个像这样的数组::

$doctor = Array(
[0] => 4
[1] => 5
[2] => 8
[3] => 35
[4] => 41
[5] => 42
)

$clinic = Array(
[0] => 1
[1] => 3
[2] => 9
[3] => 15
[4] => 19
[5] => 20
)

现在我想像这样添加这些数组

 $all = array(
 [0] => 4
 [1] => 1
 [2] => 5
 [3] => 3
 [4] => 8
 [5] => 9
 [6] => 35
 [7] => 15
 [8] => 41
 [9] => 19
 [10] => 42
 [11] => 20

我试过这个,但这不是我预期的输出:

$all = array_merge( $doctor , $clinic );

任何解决方案?

由于

1 个答案:

答案 0 :(得分:1)

你可以使用for循环来做到这一点

$doctor_size=sizeof($doctor);
$clinic_size=sizeof($clinic);
$all=[];
$size=$doctor_size;
if($doctor_size<$clinic_size){
$size=$clinic_size;
}
for($i=0;$i<$size;$i++){
if(isset($doctor[$i])){
$all[]=$doctor[$i];
}

if(isset($clinic[$i])){
$all[]=$clinic[$i];
}

}

如果数组的长度不同, 尝试

{{1}}