0

我在 CakePHP 框架内工作,并且有以下多维数组,我需要按值排序。我正在使用usort,但无法弄清楚如何按[ForumPost].

这是数组:

Array
( 
    [0] => Array
        (
            [ForumPost] => Array
                (
                    [id] => 174
                    [forum_id] => 81
                    [user_id] => 39
                    [title] => A test post
    [1] => Array
        (
            [ForumPost] => Array
                (
                    [id] => 64
                    [forum_id] => 208
                    [user_id] => 36
                    [title] => B test post
... etc

我的功能和调用是:

usort($array, array("ForumSearchesController", "cmp"));

function cmp($a, $b) {
    return $a['ForumPost']['title'] - $b['ForumPost']['title'];
}
4

1 回答 1

0

您不能简单地相互减去标题(字符串)并期望得到有意义的结果。字符串的等价物是调用函数strcmp

function cmp($a, $b) {
    return strcmp($a['ForumPost']['title'], $b['ForumPost']['title']);
}
于 2013-03-11T13:54:14.720 回答