2013-01-17 138 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]; 
    } 
}