PHP: combine values of two arrays, alternating

raw

code.php

1
2
3
4
5
6
7
<?php
$a = ['a1', 'a2', 'a3'];
$b = ['b1', 'b2', 'b3', 'b4', 'b5'];
$combo = array_filter(array_merge(...array_map(null, $a, $b)));
 
var_dump($combo);
 
raw

output.txt

array(8) {
  [0]=>  string(2) "a1"
  [1]=>  string(2) "b1"
  [2]=>  string(2) "a2"
  [3]=>  string(2) "b2"
  [4]=>  string(2) "a3"
  [5]=>  string(2) "b3"
  [7]=>  string(2) "b4"
  [9]=>  string(2) "b5"
}
Christian Weiske Christian Weiske
owner

History