Merge tags field and pages field to create list

I have a page called “Exhibition”. This blueprint has a Pages field to create a relationship with all artists that are in the exhibition (and do have a page on the website), and also a Tags field to add all the names of artists that are in the exhibition however do not have a page on the website.

In the frontend template I would love to merge these two fields through some sort of collection to show the list of Artist Page titles (as links) and the Artist name Tags all mixed in alphabetical order.

I am getting a bit lost though as it is dealing with both objects and arrays and I’m not having any luck reconciling that. Any help would be greatly appreciated.

One way of doing it:

<?php
$authorsFromTags = $page->tags()->split(',');
$authors = [];

foreach($authorsFromTags as $author) {
  $authors[] = [
    'slug' => Str::slug($author),
    'template' => 'note',
    'url' => false,
    'content' => [
      'title' => $author,
    ]
  ];
};
$authors = Pages::factory($authors, $page);
$authorsFromPages = $page->notes()->toPages();
$allAuthors = $authorsFromPages->add($authors)->sortBy('title');
?>

<ul>
<?php foreach($allAuthors as $author): ?>
  <?php if($author->url()): ?>
  <li><a href="<?= $author->url() ?>"><?= $author->title()->html() ?></a></li>
  <?php else: ?>
    <li><?= $author->title(); ?></li>
  <?php endif; ?>
<?php endforeach; ?>
</ul>

Alternative with arrays:

<?php

$authorsFromTags  = array_map(fn ($author) => '', array_flip($page->tags()->split()));
$authorsFromPages = $page->notes()->toPages();

$titles = $authorsFromPages->pluck('title', ',');
$urls  =  $authorsFromPages->pluck('url', ',');
$authors = array_combine($titles, $urls);

$allAuthors = array_merge($authorsFromTags, $authors);
ksort($allAuthors);

?>
<ul>
<?php foreach($allAuthors as $title => $url): ?>
  <?php if($url): ?>
  <li><a href="<?= $url ?>"><?= $title ?></a></li>
  <?php else: ?>
    <li><?= $title; ?></li>
  <?php endif; ?>
<?php endforeach; ?>
</ul>