我有一个模板,列出了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()
按字母顺序列出。我遇到麻烦的是将适当的图像与他们的名字联系起来。
答案 0 :(得分:0)
我认为usort可以帮助您
<?php usort($childCategories, function($a, $b) {
return strcmp($a->getName(), $b->getName());
}) ?>
<?php foreach ($childCategories as $cc): ?>
...