合并2个阵列


Merging 2 Arrays

可能重复:
将3个阵列组合为一个阵列

我有以下阵列:

$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[]?。

php的array_merge

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

也许和array_pad组合?

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

第一位很简单,只需使用array_merge((构建组合数组即可。

第二个位需要任意排序,因此需要使用usort((根据回调函数中实现的规则对数组进行排序。

订单真的很重要吗?

工作编码板-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];
  }
}