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 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)