donglie1898 2012-05-02 09:46
浏览 21
已采纳

如何对CakePHP多维数组进行排序?

Find statements in CakePHP produce an array structured as below. I've already ordered my search to produce the result set shown (ordered by combined_score). Now, I'd like to apply a sorting function on the data to sort by "average_votes". See "from this:"/"to this:" below.

I'd really appreciate any suggestions.

From this:
Array
(
[0] => Array
    (
        [Vehicle] => Array
            (
                [id] => 52
                [user_id] => 101
                [name] => Ford
                [total_votes] => 5
                [average_votes] => 3.8
                [combined_score] => 19
            )

    )

[1] => Array
    (
        [Vehicle] => Array
            (
                [id] => 48
                [user_id] => 101
                [name] => Nissan
                [total_votes] => 6
                [average_votes] => 5
                [combined_score] => 2
            )
    )
)

To this:
Array
(
[0] => Array
    (
        [Vehicle] => Array
            (
                [id] => 48
                [user_id] => 101
                [name] => Nissan
                [total_votes] => 6
                [average_votes] => 5
                [combined_score] => 2
            )
    )   

[1] => Array
    (
        [Vehicle] => Array
            (
                [id] => 52
                [user_id] => 101
                [name] => Ford
                [total_votes] => 5
                [average_votes] => 3.8
                [combined_score] => 19
            )

    )

)
  • 写回答

2条回答 默认 最新

  • dqwd71332 2012-05-02 10:28
    关注

    Depending on what you're actually trying to achieve you can still do this at the database level in cake, like so:

    $this->Vehicle->find('all',array('order' => array('Vehicle.combined_score' => 'asc', 'Vehicle.average_votes' => 'desc')));
    

    Which will first sort by combined score, and then sort by average votes

    The second option is to use the cakephp Set class, like so:

    $results = Set::sort($results, '{n}.Vehicle.average_votes', 'desc');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问