Pass additional page content while merging structure fields

I’m merging structure fields (events) from different pages to show an overview of all events on a page, sorted by date. Each event has an image, which is saved outside the structure field, but in the page.
But now I can’t find a way to display the event with the corresponding image while looping through the new collection. How can I pass the image while the merging is happening, so I can keep the relation between both?

I’m using this as a plugin for the merging:

<?php
use Kirby\Cms\Structure;

function createNewStructure($pages, $field)
{
  $structure = new Structure();

  foreach ($pages as $p) {
    $structure->add($p->$field()->toStructure());
  }

  return $structure;
}

And get the events like this:

<?php $pageCollection = $someCollection; ?>
<?php $mergedEvents = createNewStructure($pageCollection, 'events'); ?>
<?php foreach ($mergedEvents->sortBy('dateStart', 'asc') as $event): ?>
  <?= $event->name() ?>
  <?php $thumb = // How to get the image from the related page in $pageCollection? ?>
  <img src="<?= $thumb->url() ?>">
<?php endforeach ?>

Thanks for any tip !

How are the images assigned to each item? Is there a field in the structure field that contains the image name?

You can access the page that is associated with each structure object using the parent() method, in your case:

$parent = $event->parent();
dump($parent);

should give you the page object.

Perfect, that works !
The image is assigned outside the structure field, so I couldn’t figure out how to access it.
Thanks a lot