我正在尝试将数组从高到低排序。
查询是在Joomla模块中编写的,并且是这样的:
$authors = array();
if (count($rows))
{
foreach ($rows as $row)
{
$author = JFactory::getUser($row->created_by);
$author->link = JRoute::_(K2HelperRoute::getUserRoute($author->id));
$query = "SELECT id, gender, description, image, url, `group`, plugins FROM #__k2_users WHERE userID=".(int)$author->id;
$db->setQuery($query);
$author->profile = $db->loadObject();
if ($params->get('authorAvatar'))
{
$author->avatar = K2HelperUtilities::getAvatar($author->id, $author->email, $componentParams->get('userImageWidth'));
}
if (K2_JVERSION != '15')
{
$languageCheck = '';
if ($application->getLanguageFilter())
{
$languageTag = JFactory::getLanguage()->getTag();
$languageCheck = "AND i.language IN (".$db->Quote($languageTag).", ".$db->Quote('*').") AND c.language IN (".$db->Quote($languageTag).", ".$db->Quote('*').")";
}
$query = "SELECT i.*, c.alias as categoryalias FROM #__k2_items as i
LEFT JOIN #__k2_categories c ON c.id = i.catid
WHERE i.created_by = ".(int)$author->id."
AND i.published = 1
AND i.access IN(".implode(',', $user->getAuthorisedViewLevels()).")
AND ( i.publish_up = ".$db->Quote($nullDate)." OR i.publish_up <= ".$db->Quote($now)." )
AND ( i.publish_down = ".$db->Quote($nullDate)." OR i.publish_down >= ".$db->Quote($now)." )
AND i.trash = 0 AND created_by_alias='' AND c.published = 1 AND c.access IN(".implode(',', $user->getAuthorisedViewLevels()).") AND c.trash = 0 {$languageCheck} ORDER BY created DESC";
}
else
{
$query = "SELECT i.*, c.alias as categoryalias FROM #__k2_items as i
LEFT JOIN #__k2_categories c ON c.id = i.catid
WHERE i.created_by = ".(int)$author->id."
AND i.published = 1
AND i.access <= {$aid}
AND ( i.publish_up = ".$db->Quote($nullDate)." OR i.publish_up <= ".$db->Quote($now)." )
AND ( i.publish_down = ".$db->Quote($nullDate)." OR i.publish_down >= ".$db->Quote($now)." )
AND i.trash = 0 AND created_by_alias='' AND c.published = 1 AND c.access <= {$aid} AND c.trash = 0 ORDER BY created DESC";
}
$db->setQuery($query, 0, 1);
$author->latest = $db->loadObject();
$author->latest->id = (int)$author->latest->id;
$author->latest->link = urldecode(JRoute::_(K2HelperRoute::getItemRoute($author->latest->id.':'.urlencode($author->latest->alias), $author->latest->catid.':'.urlencode($author->latest->categoryalias))));
$query = "SELECT COUNT(*) FROM #__k2_comments WHERE published=1 AND itemID={$author->latest->id}";
$db->setQuery($query);
$author->latest->numOfComments = $db->loadResult();
if ($params->get('authorItemsCounter'))
{
if (K2_JVERSION != '15')
{
$languageCheck = '';
if ($application->getLanguageFilter())
{
$languageTag = JFactory::getLanguage()->getTag();
$languageCheck = "AND language IN (".$db->Quote($languageTag).", ".$db->Quote('*').")";
}
$query = "SELECT COUNT(*) FROM #__k2_items WHERE {$where} published=1 AND ( publish_up = ".$db->Quote($nullDate)." OR publish_up <= ".$db->Quote($now)." ) AND ( publish_down = ".$db->Quote($nullDate)." OR publish_down >= ".$db->Quote($now)." ) AND trash=0 AND access IN(".implode(',', $user->getAuthorisedViewLevels()).") AND created_by_alias='' AND created_by={$row->created_by} {$languageCheck} AND EXISTS (SELECT * FROM #__k2_categories WHERE id= #__k2_items.catid AND published=1 AND trash=0 AND access IN(".implode(',', $user->getAuthorisedViewLevels()).") {$languageCheck} )";
}
else
{
$query = "SELECT COUNT(*) FROM #__k2_items WHERE {$where} published=1 AND ( publish_up = ".$db->Quote($nullDate)." OR publish_up <= ".$db->Quote($now)." ) AND ( publish_down = ".$db->Quote($nullDate)." OR publish_down >= ".$db->Quote($now)." ) AND trash=0 AND access<={$aid} AND created_by_alias='' AND created_by={$row->created_by} AND EXISTS (SELECT * FROM #__k2_categories WHERE id= #__k2_items.catid AND published=1 AND trash=0 AND access<={$aid} ) ";
}
$db->setQuery($query);
$numofitems = $db->loadResult();
$author->items = $numofitems;
}
$authors[] = $author;
}
}
return $authors;
}
然后将结果传递给这些变量,最后返回authors表。我要将此表从高到低排序。我已经尝试了php函数,但是似乎没有一个起作用。
我不确定查询是否检索到项目计数器之外的其他内容并将其设置为键。但是我尝试过诸如arsort之类的功能,该功能按值而不是键排序。
$db->setQuery($query);
$numofitems = $db->loadResult();
$author->items = $numofitems;
$authors[] = $author;
arsort($authors);
任何想法都值得赞赏
谢谢
答案 0 :(得分:1)
通常,数据库查询可以通过指定顺序来进行排序
$query->order('my_table.my_column desc')
如果要对PHP数组进行排序并需要自定义排序条件,则可以将自己的排序条件与usort
一起使用。例如:
// Your comparison function:
function myComparer($object1, $object2)
{
if ($object1->item == $object2->item) {
return 0;
}
return ($object1->item < $object2-item) ? -1 : 1;
}
// Sort the array in place
usort($authors, 'myComparer');
请注意,您可以通过交换比较返回值来反转排序顺序:
return ($object1->itemCount < $object2-itemCount) ? 1 : -1;