Sort records by date inside Foreach Loop

3 posts by 2 authors in: Forums > CMS Builder
Last Post: March 28, 2018   (RSS)

By Mikey - March 26, 2018

I need to sort all my records inside of a foreach loop by date. I know this can be done using 'orderBy' => 'date ASC', but I can use that function in this scenario, and I need to sort the records by date inside, or just before the foreach loop. Anyone have any suggestions?

<?php foreach ($mediaRecords as $record): ?>
        <?php sort($record['date'] ='ASC'); ?>
       
        <?php foreach ($record['media'] as $index => $upload): ?>
        <img src="<?php echo $upload['thumbUrlPath2'] ?>" alt="<?php echo htmlencode($record['title']) ?>" />
        <?php endforeach; ?>
        
<?php endforeach ?>

Thanks,

Zicky

By Djulia - March 28, 2018 - edited: March 28, 2018

Hi,

You can try this :

<?php array_multisort(array_column($mediaRecords, "date"), SORT_ASC, $mediaRecords); ?>
<?php foreach ($mediaRecords as $record): ?>
  <?php foreach ($record['media'] as $index => $upload): ?>
  <img src="<?php echo $upload['thumbUrlPath2'] ?>" alt="<?php echo htmlencode($record['title']) ?>" />
  <?php endforeach; ?>
<?php endforeach ?>

Djulia