PHP按字母顺序排序

时间:2016-10-03 18:36:20

标签: php sorting magento2 alphabetical

我有一个模板,列出了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>

我尝试做的是根据姓名$cc->getName()按字母顺序列出。我遇到麻烦的是将适当的图像与他们的名字联系起来。

1 个答案:

答案 0 :(得分:0)

我认为usort可以帮助您

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

<?php foreach ($childCategories as $cc): ?>
  ...