An array of array1 = Array (' A ', ' B ', ' C ');
array2 = Array (' 1 ', ' 2 ', ' 3 ');
Hope to achieve results Array3 = Array (' A ', ' 1 ', ' B ', ' 2 ', ' C ', ' 3 '); This effect,
Method One:
1 functionCross_merge_array ($arr 1,$arr 2)2 {3 $arr 1=array_values($arr 1);4 $arr 2=array_values($arr 2);5 $count=Max(Count($arr 1),Count($arr 2));6 $arr=Array();7 for($i= 0;$i<$count;$i++) {8 if($i<Count($arr 1))$arr[] =$arr 1[$i]; Judge to avoid the subscript crossing9 if($i<Count($arr 2))$arr[] =$arr 2[$i]; Judge to avoid the subscript crossingTen } One return $arr; A}
Reference: https://segmentfault.com/q/1010000014216293
Method Two:
1 functionCross_merge_array ($arr 1,$arr 2)2 {3 $size=Count($arr 1) >Count($arr) ?Count($arr 1) :Count($arr 2);//the maximum number of elements to take out4 $arr=Array();5 for($i= 0;$i<$count;$i++) {6 if($i<Count($arr 1)) {7 Array_push($arr,$arr 1[$i]);//press the array into the new variable8 }9 Ten if($i<Count($arr 2)) { One Array_push($arr,$arr 2[$i]);//press the array into the new variable A } - } - return $arr; the}
Reference: http://www.songlin51.com/archives/832.html
In fact, the principle is the same:
are based on the long length of the Traverse, and then cross into the new array, in the loop you need to determine whether the subscript is out of bounds
PHP Cross Merge arrays