-1

I have a template that lists out elements in a foreach:

<?php $currentCategory = $this->getCurrentCategory();?>
<?php $childrenCategories = $this->getChildrenCategoriesCollection($currentCategory);?>
<div class="subcategory-wrap">
    <ul class="subcategory-list">
        <?php ?>
        <?php foreach ($childrenCategories as $cc): ?>
            <li class="subcategory-item">
                <a class ="subcategory-image-link"href="<?php echo $cc->getUrl() ?>"><img src="<?php echo $cc->getImageUrl() ?>" class="subcategory-image" /></a>
                <span class="subcategory-title"><a href="<?php echo $cc->getUrl() ?>"><?php echo $cc->getName() ?></a></span>
            </li>
        <?php endforeach; ?>
    </ul>
</div>

What I'm trying to do is list them out alphabetically based off of their name $cc->getName(). What I'm having trouble with is associating the appropriate images with their names.

Paul
  • 101
  • 2
  • 1
    Possible duplicate of [Sort array of objects by object fields](http://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields) – Mulan Oct 03 '16 at 18:41

1 Answers1

0

I think usort can help you

<?php usort($childCategories, function($a, $b) {
  return strcmp($a->getName(), $b->getName());
}) ?>

<?php foreach ($childCategories as $cc): ?>
  ...
Mulan
  • 129,518
  • 31
  • 228
  • 259