hi i have the following code from a template.
<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>
I would like to sort the sub category items (which are cities subdivided by state further up in the code.
i thought i could just sort the following array $category->getChildren() but it doesnt work. so i did an echo on it and it said array, so i did var_dump on that array and got a bool(true) on it. when i tried other ways (print_r) of outputting it it crashed the page.
i dont understand arrays very well so could someone explain what is this array that is not an array? how can i sort the city list?
thanks!
I don't really understand what the problem was with trying to print the array, but I would think defining a custom sort with usort()
like this would be what you're looking for:
<?php
function compareChildren ($a, $b) {
return strcmp($a->name, $b->name);
}
$children = $category->getChildren();
usort($children, 'compareChildren');
foreach ($children as $child) {
// ...
}
Here's a working example on codepad .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.