Check if layout is last in set

This is probably simple but I can’t get it to work - can I check if a layout is the last in the set? So if I have this:

<?php foreach ($page->pageBuilder()->toLayouts() as $layout): ?>

<?php endforeach ?>

Can I check within the loop if it is the last layout element, and do something different with it?

$layouts = $page->pageBuilder()->toLayouts();
foreach ($layouts as $layout): ?>

<?php if ( $layout === $layouts->last() ) {
  // do stuff

<?php endforeach ?>

That is just the ticket, than you! I was attempting if($layout->last()), which didn’t work! Thanks very much for your patient help once again :slightly_smiling_face: