2013-01-17 134 views
-3

可能重复:
Combing 3 arrays into one array合并2个阵列

我有以下阵列:

$front = array("front_first","front_second"); 
$back = array("back_first", "back_second", "back_third","back_fourth"); 

什么,我试图做的是输出合并它们如此像这样会导致:

$final = array(
    "back_first", 
    "front_first", 
    "back_second", 
    "front_second", 
    "back_third", 
    "front_second", 
    "back_fourth", 
    "front_second" 
); 

我怎样才能让它重复在最短的数组中的最后一个值,以便它可以合并成一个没有空值的$final[] ?.

回答

0

php的array_merge

$final = array_merge($front, $back); 

,也许与array_pad组合?

$front = array_pad($front, count($back)-1, 'second_front'); 
$final = array_merge($front, $back); 
0

的第一位是容易的,只要使用array_merge()来构建你的组合阵列。

第二位需要任意排序,因此需要使用usort()根据您在回调函数执行规则的数组进行排序。

订单真的很重要吗?

+0

是在orderis重要。 – thindery

0

工作Codepad - bgIYz9iw

$final = array(); 
for($i = 0; $i < 4; $i++) { 
    if($i > 1) { 
    $final[] = $back[$i]; 
    $final[] = $front[1]; 
    } 
    else { 
    $final[] = $back[$i]; 
    $final[] = $front[$i]; 
    } 
}