0

嗨,我有来自模板的以下代码。

<ul class="sub-categories">
    <?php

        foreach ($category->getChildren() as $child) {
            if (!$child->totalItemCount()) continue;
            $link = $this->app->route->category($child);
            $item_count = ($this->params->get('template.show_sub_categories_item_count')) ? ' <span>('.$child->totalItemCount().')</span>' : '';
            echo '<li><a href="'.$link.'" title="'.$child->name.'">'.$child->name.'</a>'.$item_count.'</li>';
        }

    ?>
</ul>

我想对子类别项目(在代码中进一步按州细分的城市)进行排序。

我以为我可以对以下数组进行排序 $category->getChildren() 但它不起作用。所以我对它做了一个回声,它说数组,所以我在那个数组上做了 var_dump 并得到了一个 bool(true) 。当我尝试其他输出方式(print_r)时,它使页面崩溃。

我不太了解数组,所以有人可以解释这个不是数组的数组是什么吗?我如何对城市列表进行排序?

谢谢!

4

1 回答 1

1

我真的不明白尝试打印数组的问题是什么,但我认为用usort()这样的方式定义自定义排序将是您正在寻找的:

<?php

function compareChildren ($a, $b) {
    return strcmp($a->name, $b->name);
}

$children = $category->getChildren();
usort($children, 'compareChildren');

foreach ($children as $child) {
    // ...
}

这是 codepad 上的一个工作示例

于 2012-07-12T18:20:26.360 回答