Mix two tag fields together

How would i mix the tags from two field together so they alternate on the front end?

Tag field a:

Tag1, Tag2, Tag3

Tag field b:

TagA, TagB, TagC

Desired output on the front end:

Tag1, TagA, Tag2, TagB, Tag3, TagC

The only purpose is to make it easier for the content editors to enter without having to think too much about the order (the words basically have opposite meanings to the ones in the other box.)

The boxes wont necessarily have the same number of tags in, if that makes any difference to the logic, but should be mixed as best as possible like above.

Should the values from field a always be the starting array or does order not matter?

Yes, the first tag should be from field A, and in the order entered into the boxes. They just need to be alternately mixed.

$aTags = ['tagA', 'tagB', 'tagC', 'tagE', 'tagF'];
$nTags = ['tag1', 'tag2', 'tag3', 'tag4'];

$arrayCombined = array_map(null, $aTags, $nTags);
$return = [];
array_walk_recursive($arrayCombined, function($a) use (&$return) { $a !== null ? $return[] = $a: null; });


Awesome :slight_smile: Thank you so much @texnixe . I knew it was a head scratcher…

Yes, took a while to find the best option, I recall I once had to that in Pascal…

You can also achieve that by just looping through the first array like this, but the above seems more elegant.

$aTags = ['tagA', 'tagB', 'tagC', 'tagE', 'tagF'];
$nTags = ['tag1', 'tag2', 'tag3', 'tag4'];
$diff = count($nTags) - count($aTags);
$combinedArray = [];
foreach ($aTags as $key => $tag) {
  $combinedArray[] = $tag;
  isset($nTags[$key]) ? $combinedArray[] = $nTags[$key] : null;
if ($diff > 0) {  
    $combinedArray = array_merge($combinedArray, array_slice($nTags, -$diff, $diff));  

Maybe there are even better ways, I don’t know. It’s the best I could come up with.

Don’t fret, its magnificent. :slight_smile: Thanks alot.