joomla php从高到低排序数组[重复]

Posted

技术标签:

【中文标题】joomla php从高到低排序数组[重复]【英文标题】:joomla php sort array from high to low [duplicate] 【发布时间】:2019-06-28 17:39:25 【问题描述】:

我正在尝试从高到低对数组进行排序。

查询是在 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;

然后将结果传递给这些变量,最后返回作者表。这张表我想从高到低排序。我已经尝试过 php 函数,但它们似乎都不起作用。

我无法确定查询是否检索到项目计数器之外的其他内容并将其设置为键。但是我已经尝试过诸如 arsort 之类的函数,它是按值而不是键排序的。

$db->setQuery($query);
$numofitems = $db->loadResult();
$author->items = $numofitems;
$authors[] = $author;
arsort($authors);

任何想法都值得赞赏

谢谢

【问题讨论】:

当您对 Joomla 有疑问时,请在Joomla Stack Exchange 发帖。您应该使用 Joomla 的查询构建方法构建您的查询。 您的代码可以重构以压缩您的条件查询构建。 $where 是个谜。事实上,您似乎可以将一些查询组合起来,以避免大部分复杂的代码。 这个页面没有被赞成的答案:***.com/q/24956420/2943403 @mickmackusa 当然这个类没有优化,但它是 joomla 的内置模块,所以我没有写它。在我发布之前,相信我,我尝试了一切。但我不是一个健全的开发人员,我也没有使用数组(键、值、对象)的经验。因此,根据答案,我无法区分是否应该将 $author 替换为 $a 或 $items 替换为 $a 不要比较它们的字符串值,使用 jspcal 的解决方案。此外,不要在这里(永远)共享代码图像,我们总是希望使用文本以防我们想要复制粘贴。 3v4l.org 很好用。 【参考方案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;

【讨论】:

感谢您的建议,但上面的代码给了我一个空白页警告:usort() 期望参数 1 是数组,对象在 /home/proteino/public_html/modules/mod_k2_tools/helper 中给出。第 167 行的 php 致命错误:无法在 /home/proteino/public_html/modules/mod_k2_tools/helper.php 中重新声明 myComparer()(之前在 /home/proteino/public_html/modules/mod_k2_tools/helper.php:156 中声明) 156 您应该将一个数组传递给 usort。并且不要两次包含该函数。 我尝试了所有可能的组合 usort ($authors, 'myCoparer') usort ($authors[], 'myCoparer') usort ($author, 'myCoparer') usort ($author[], 'myCoparer') 没有任何效果。 如果我只是使用 arsort,表格正在排序,但不是根据 $author->items = $numofitems;但根据别的东西 你拼错了“myComparer”。

以上是关于joomla php从高到低排序数组[重复]的主要内容,如果未能解决你的问题,请参考以下文章

php 数组函数

PHP中的排序函数sortasortrsortkrsortksort区别分析

数组排序

PHP中的排序函数sortasortrsortkrsortksort区别分析

PHP中的排序函数sortasortrsortkrsortksort区别分析

没有内置的从高到低排序[关闭]