[英]printing out an array alphabetically
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. 我以为我可以对下面的数组$ category-> getChildren()进行排序,但是它不起作用。 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.
所以我在它上面做了一个回声,并说是数组,所以我在该数组上做了var_dump并在上面得到了bool(true)。 when i tried other ways (print_r) of outputting it it crashed the page.
当我尝试其他方式(print_r)输出它时,它使页面崩溃了。
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: 我真的不明白尝试打印数组的问题是什么,但是我想用
usort()
定义自定义排序就是您要寻找的东西:
<?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 . 这是键盘上的工作示例 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.