【问题标题】:php array multisort by inner arrays value [duplicate]php数组按内部数组值多重排序[重复]
【发布时间】:2015-09-03 07:00:19
【问题描述】:

我正在尝试在 PHP 中对投票列表进行排序。

列表是一个数组,包含类对象:

Array
(
    [0] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => bbh - bghs dsdw
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 6
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => bbh--0
                )

        )

    [1] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => aaa - bbb
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 4
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => aaa--0
                )

        )

    [2] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => wdewv - qwdqs
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 3
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => wdewv--0
                )

        )

    [3] => VotedSong Object
        (
            [title] => SimpleXMLElement Object
                (
                    [0] => Hsg and fdSv - aGamaama
                )

            [votes] => SimpleXMLElement Object
                (
                    [0] => 2
                )

            [key] => SimpleXMLElement Object
                (
                    [0] => hsgandfdsv--0
                )

        )
)

我设法通过->key 在那里进行排序,工作正常:

usort($votedsongs, function ($a, $b) { return $b->votes - $a->votes; });

但是在这之后,我仍然需要另一个排序函数来对->title投票数相同的歌曲进行排序。

我已经找到了一些可以处理类似问题的解决方案,但这些对我不起作用。

对此有何想法?

【问题讨论】:

  • ` if (!($r = $b->votes - $a->votes)) $r = next_rules;返回 $r;`
  • 请解释一下,我不知道如何实现。
  • if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r;
  • 所以我替换了我的 usort 函数,例如:usort($votedsongs, function ($a, $b) { if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r; }); 对吗?可悲的是,这并没有奏效..
  • title改成titel,结果是什么?

标签: php arrays class sorting


【解决方案1】:

听起来您想按votestitle(拼写错误为titel)对数组中的VotedSong 对象进行排序。如果是这样,这可能会起作用:

usort($votedsongs, function ($a, $b) {
    if ($b->votes == $a->votes) {
        return ($a->title < $b->title) ? -1 : 1;
    }
    return $b->votes - $a->votes;
});

【讨论】:

  • 看起来不错,但它不起作用,不知道为什么..我已经找到了解决方案,不过还是谢谢。
【解决方案2】:

感谢 splash58 提供的解决方案:

if (!($r = $b->votes - $a->votes)) $r = strcmp($b->title, $a->title); return $r;

我将字母排序修改为不区分大小写并切换$a-&gt;title$b-&gt;title - 就是这样:

usort($votedsongs, function ($a, $b) { 
    if (!($r = $b->votes - $a->votes)) $r = strcasecmp($b->title, $a->title); return $r; 
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-28
    • 1970-01-01
    • 2018-11-11
    • 2013-03-11
    • 2013-11-01
    相关资源
    最近更新 更多