交替合并两个数组 [英] Merge two arrays alternatively
本文介绍了交替合并两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要的是efficient
(无循环)方式以first element of the resulting array
是first element of the first array
,the second element of the resulting array
是the second element of the second array
(或者)...的方式合并数组的方式...
What I want is an efficient
(without looping) way to merge arrays in the way that the first element of the resulting array
is the first element of the first array
, the second element of the resulting array
is the second element of the second array
(alternatively)... etc
示例:
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$resultingArray = array(1, 2, 3, 4, 5, 6);
推荐答案
假定两个数组的长度相同.
assuming both arrays have the same length.
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$new = array();
for ($i=0; $i<count($arr1); $i++) {
$new[] = $arr1[$i];
$new[] = $arr2[$i];
}
var_dump($new);
这篇关于交替合并两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文