$categories = PostCategoryQuery::create()->find();
$categories_array = [];
foreach ($categories as $k => $category):
$posts_count = PostQuery::create()->filterByPostCategory($category)->count();
$categories_array[$k]['category'] = $category->getName();
$categories_array[$k]['count'] = $posts_count;
endforeach;
uasort($categories_array, "sortByCount");
function sortByCount($a, $b) {
return $a['count'] > $b['count'] ? -1 : 1;
}
I want to get 'categories' array order by numbers of its related posts, I think there must be a way to make my code shorter, hoping get some suggestions, thanks~
If in your ORM you could use a simple query with group by you simply could perform a select like
and then your code will be reduced to the query..